- Home /
How do I access a gameobjects variables in a seperate script?
I have a script attached to a popup list. In this script I want to change texture on a different game object. I think I have the scripts down, I just don't know how to access the renderer of the other gameobject since the script is not attached to it.
Do I need to put a script on each of those objects that just instantiates a variable for the gameobject and assign that to a public variable in the Start section or is there an easier way?
Do you know in advance which GameObject it will be? Is it a permanent part of your scene? If not, what other features might identify this object as being relevant to the object (e.g. name, tag, etc.)?
The are permanent parts of the scene. I am just trying to change the texture on them based off the selection in the drop down.
Answer by paulaceccon · Oct 23, 2012 at 08:11 PM
Like Sundar said, I think what you want is use GameObject.Find and GameObject.GetComponent (http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html )(http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html)
Then you can do something like Sundar said.
If, besides this, you want to access some scripts variables, and not just get components, you have to make this variables public (if there is any specifying it, they you be private) or, to be follow programming principles, make set and get functions for the variables that you want access.
Answer by phodges · Oct 21, 2012 at 07:37 PM
Based on above comments, since you know the identity of the gameobject in advance, you could add a gameobject property to your popup script and set that to the relevant gameobject in the inspector.
Answer by Sundar · Oct 23, 2012 at 07:52 PM
Try this
GameObject.Find("goName").GetComponent<MeshRenderer>().material.mainTexture = newTexture;