Sound designer/Game Developer based in South Yorkshire, UK

Report RSS c# - copied, not another pointer?

Posted by on

unity code:-

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	
	
	
	class qVector2
	{
		public float x;
		public float y;
		
		public qVector2(float a = 0.0f, float b = 0.0f)
		{
			x = a;
			y = b;
		}
	}
	
	void Start ()
	{
		Vector2 test = new Vector2(3.4f,2.2f);
		
		Vector2 test2 = test;//test2 is new'd and whole of test is copied into it - each Vector2 points to a different memory area.
		
		test2.x = 100.0f;
		
		Debug.Log (test.x);//still = 3.4
		
		
		
		
		
		
		qVector2 qtest = new qVector2(3.4f,2.2f);
		
		qVector2 qtest2 = qtest;//done as pointer, no data copy
		
		qtest2.x = 100.0f;
		
		Debug.Log (qtest.x);//now 100
		
		
	}
	

}
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: