- Home /
Reference string from another gameobject and not directly from the script itself?
I have a gameobject with a script that has a string variable. I have duplicated this gameobject four times.
I want to reference this string from each duplicate from another script, but I can't seem to do it correctly because the four gameobjects are referencing a single script.
Rather than having to duplicate this script four times along with the gameobject, is there a way to reference a variable from a gameobject rather than directly from the script?
Are you sure about your assumption that the four different GameObjects are all referencing a single script (is the variable static)?
If you have four different GameObjects each with a script that has a string var, then you have four instances of that script each uniquely attached to its own GameObject, unless you've gone out of your way to NOT do that.
Each script is a separate instance of the script. What you need to do is find some way to differentiate the GameObjects/script instances.
Without some more information about how the scene is set up it will be hard to help you find a solution. Are your duplicated object placed through code or did you manually duplicate them?
Answer by LessThanEpic · Jan 24, 2015 at 12:19 AM
It sounds like you want to make that string a static variable.
Your answer

Follow this Question
Related Questions
Deserializing a JSON file yields 0 when referencing the variables in another script 2 Answers
How to access a non static variable by another static variable? 1 Answer
variables within variables? 2 Answers
Storing script names in a variable 1 Answer
Properly referencing a variable from another script 1 Answer