- Home /
Access variable from multiple scripts
How to access a variable from multiple scripts? How to make the scripts relative? For example, I have a GuiText and I want to make only one GuiText which will change the text eventually as the selection changes. Any ideas? Thanks. :)
Answer by DaveA · Jul 31, 2011 at 04:56 AM
RTM at : http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Answer by BerggreenDK · Aug 01, 2011 at 10:38 AM
You could also just make an ugly STATIC version of your variables.
Like:
public static string myGlobalValue = "Hello World";
I usually have a "Global.cs" scriptfile for such ugly variables, because they are faster to access. Eg. if I have a serveraddress. I just keep a:
public static string urlServer = "http://www.someserver.com/";
Then when I need this value in subscripts that access assets or stores a hiscore, I can use that top-level variable in every call like this:
WWW w = new WWW( Global.urlServer + "/somefile.txt");
I dont know if thats too ugly, but it solves my problem perfectly.
So does this mean that if it's static it cannot be changed right? Like it can be one and only one number or string?
"static" variables means that its globally available without the need of being instantiated first as in a normal class, "const" is constant values. Think you mix these two up. Did at quick Google on "C# static", here is the result: http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Changing variables in GUI? 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Limit on GUI Components? 0 Answers
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers