How do i work on an object by refference? I mean, ive a class like below:
class Object() {
name = “string here”
}
mono class () {
Object obj = new Object();
Now how do i make another tempObj and ref to the obj properties?
Object tempObj = obj ??
There is no such thing as pointers.
And assigning an object is automatically a reference to the object.
So if you do Object tempObj = obj, then tempObj points to the same thing as obj does automatically
class instance are not passed by value (ie not a copy clone)
So, you mean if i change tempObj.name, it will also change obj.name?
Thats what i need to do.
Thanks.
So, just to make things clear for me now, are the below statements correct?
Object obj = new Object(); //Makes a new object
Object tempObj = obj; // tempObj is equal to obj and if i change tempObj.name, obj.name will also change, or if i change obj.name, tempObj.name will also change
Object tempObj2 = new Object();
tempObj2 = obj; //this is a completely new instance and independent from obj but takes initial values from obj
the last line is incorrect.
when you assign obj, the previous stored object reference is no longer referenced and as this was the only reference to this object it will be garbage collected.
tempObj2, like tempObj2 will point to obj and any changes done on tempObj2 will affect tempObj and obj and vice versa
No idea where you happen to have such problems with cause UnityScript and Boo work exactly the same so this should not be new to you
Yes, your statements are right Aubergine, just give it a try and see it in the debugger.
@Dreamore
Really? I tought it wouldn’t go this way, maybe I need more time programming
if you want to clone the content of a class instance you explicitely have to copy the stuff. otherwise you only reference it.
Perhaps you are mixing up here Class and Struct cause struct are passed by value, if you assign them, you copy the stuff.
Also unity uses Properties for various things, which will lead to similar situations
There is no way in c# to referrence an object with 2 different tempObj then? This is not good.
You can reference an object with two different variables. You can’t reference two different objects with the same variable.
Well, im new to all c#, unityscript and boo
Anyways, i got it now. Thanks for all the help.
think of a variable to a class instance as a person always pointing a finger towards a point of interest (that point of interest then is the object you would assign).
by using this person you can always find the object, no matter where it is, to do something with it.
But no matter what you do, the person will not become the object it is pointing at.
And it seems you missunderstood what I mentioned above:
with tempObj2 = obj, the original “new Object()” you pointed to, will be lost. You will not lose the class instance behind obj, instead it will now have 3 variables (obj, tempObj, tempObj2) leading to it
As Dreamora said, this is incorrect. If you want to “clone” an object you have to manually copy the properties of the first object to the new object. Some classes will have methods to do this automatically for you… Some .NET objects can be cloned using .Clone()