Reputation: 1
How can I change GameObjects by clicking the UI buttons in Unity?
For example: there are 4 different color cubes (red, blue, green, pink) in my scene, and I have given some actions to each cube.
I want to select each cube and do those actions by clicking on each respected button. An example is shown here
Upvotes: 0
Views: 847
Reputation: 533
use the onClick() function to trigger some events. First write the needed function in the scripts and then add the script to the gameobject. On inspecting the object you can see the onClick(). Add the respective function.
Upvotes: 1
Reputation: 134
Assign Each Cube a seperate script pseudo code:
public class xyz{
public GameObject _Cube;
public void colorChange(){
//your code here
}
}
or for single script
public class xyz{
public GameObject[] _Cube;
public void colorChange(int index){
//your code here
}
}
Now Create (For seperate scripts)Buttons or (For Single Script) Single Button. In the Button we can have OnClick Function. just assign the script and select method you want to invoke thru button click.
If you are not familiar with Unity. Go See Brackeys On YouTube
Upvotes: 0