- Home /
set gameobject for another script
I have a GUI script and when a button is pushed I want it to take an object from the top and set it as the gameobject on the other script. In the GUI I have declared gameobjects for each button when you push this button it find the script I want to set that object as part of.
My problem is what do I use to set the object my button knows as the gameobject of the other script.
This is my GUI script when you push the button this is the code it does.
public GameObject Ground;//This refernces the object the script is on.
public GameObject dirt;//This is the object I want to send over.
void OnGUI()
{
if (GUI.Button(new Rect(120, 140, 75, 75), button1Text))//This is the button.
{
Ground.GetComponent<myScript>().enabled = true;
//This finds the script im wanting to edit.
}
}
I want to send that object over to this script and set it as the currentobject.
public GameObject currentObject;
You need to get the game object containing that script and then you need to use GetComponent() to get a reference to that instance of the script.
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
How to keep my cursor inside window boundaries 0 Answers
MonoDevelop stuck in solution 0 Answers