- Home /
Set variable and then keep it that way
Hello, what I'm trying to do is set a variable from another variable and then when the variable the other variable is set to changes the first variable doesn't. That sounds to complicated here's a simpler version.
Let's say i have 2 int variables the first one and the second one is:
var int1 : int;
var int2 : int;
now i have a string variable.
var stringname : string;
but stringname is named after the 2 int variables and is formatted like this: "`stringname[int1 value, int2 value]`" but after stringname is named, i then want to keep it named that way even after int1 & int2 values are changed. I know it sounds complicate, sorry :/ i just can't figure this out for the life of me. Thanks in advance. ;D
You are going to have to provide more source and perhaps an example. Some variables refer to data by reference, some refer to the data by value (actually contain the data). Everything you list above refers to the data by value (or in the case of strings an immutable reference). So the behavior should be what you want. So we need a bit of source that shows the behavior that you don't want to understand the problem you are trying to solve.
Answer by Yokimato · Jul 02, 2013 at 07:09 PM
If I understand your question:
Raw data types (i.e non-objects) work as you intend.
var x : int;
var y : int;
var z : string;
x = 1;
y = 2;
z = "stuff" + x + y; // stuff12
x = 100; //x changes but z remains stuff12
However, objects behave differently since you're really dealing with a pointer to the object and thus you just assign the pointer (i.e you know have 2 pointers to the same object)
var x : GameObject;
var y : GameObject;
x = GameObject.Find(...);
z = x; // both z and x point to the same object
// and thus any changes to x apply to z and vice versa
If you didn't want this functionality, you would need to make a copy of the object (creating a new instance). This way, you end up with x
pointing to the original object and z
points to its own, copied object.
Your answer

Follow this Question
Related Questions
parseInt givin' me issues! 2 Answers
How to convert a string to int array in Unity C# 1 Answer
How to turn a String to an Int? 5 Answers
Convert int to string and back again 2 Answers