- Home /
When editing a scripts variable in another script, does the original script update?
If you pull a number variable from a script, and in another script, say subtract amount from variable, will that number be updated in the original script?
To go in more depth, I have a int variable from script1. I am subtracting from that variable in script2. So, how do I make script1 updated to the new int? The variable from script1 is defined as a new variable in script2 if that matters.
Answer by Owen-Reynolds · Jan 23, 2013 at 03:22 AM
If you want to change a variable in another script, just change it: cowScript.x = cowScript.x + 2;
If you copy it, then changing the copy won't do anything to the original. But, a common trick to save typing is to copy, change and copy back:
x2 = cowScript.x;
if(x2<5) .... x2+=6; if(x2>10) .... // don't have to write cowScript.x so much
cowScript.x = x2; // copy back when done
But, some variables are actually just links (pointers.) For example, transforms. If you copy a transform, you don't really. You just get another link to the original transform (yes, this is a very confusing rule.) So this works:
var tt : Transform = cowScript.tail;
tt.localPosition = Vector3.zero;
// same as cowScript.tail.localPosition = Vector3.zero;
OH$$anonymous$$EHGERD! Not what I did really, but you ignited some logic in me, and it works now! woo! thanks!
Cannot update original variable from script1 either.I can read the variable with getcomponent function in script2, but can not change the original value(from script1). C# - unity answer please. I need to change int variable.
Found it.If one makes a variable in script1 public static (public static int myglobalvariable - for instance),they dont even have to use GetComponent function to read it in script2.Just type script1name.myglobalvariable=somenumber;(to change it globaly(for entire game)) in script2.
Statics usually end up causing more trouble than they save. They often need to be ripped out later and redone as a regular variables.
The whole process for normal vars is: make a var to link to the script: public otherScript SS;
. Then make SS actually link. Either drag in the object, or SS = GameObect.Find("abc").GetComponent("otherScript");
Then use SS: SS.x = 8;
.
Answer by Catlard · Jan 23, 2013 at 02:52 AM
Hey there, bpears. There are two ways to get a variable from another location -- by reference and by value. When you take a variable from a script1 normally (by reference), without using the "ref" keyword, you get a copy that has no effect on the original variable. But if you use the ref keyword, the variable's location in memory will be passed as well -- so the original value of the int variable in script1 can be changed in script2.
This should explain the syntax: http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx
I see, but I am using unityscript, and a search led me to believe there is no such thing in unityscript. So somehow I need to return the value back or something similar... hmmm... thanks though!
Your answer
Follow this Question
Related Questions
GetComponent from class 1 Answer
Issues accessing a variable in another script 0 Answers
Passing a Script Name to a Function 2 Answers
Assigning a unity component variable issue 1 Answer
AddComponent with parameter variable 2 Answers