Toggle between List of Objects

I think this is something very basic but I don’t really know where to search for. I for example have 4 spheres. If I click on Sphere 1 I want it to be RED. If I click on Sphere2 I want Sphere2 to be RED but Sphere1 back to it’s original color.
I think this has to do with lists and for each loop? but not sure

Hello.

when you say “click” you mean the user when playing? or you in the editor?

What you want to do is to change the material of the object.

GetComponent<Renderer>().material= someMaterial;

Bye

Yes I got that working. But I want to toggle between the objects.

Another example:
I have 4 cubes and 4 buttons. If click button 1 I want cube 1 to be Hidden. If I press button 2 I want cube 2 to be hidden BUT cube 1 to be vissible again. I can do this all manually like if button 1 is pressed then set cube 2 cube 3 cube 4 to vissible. But that takes a lot of work if you have like 100 objects.

You can assign this same method to all the buttons :

[SerializeField] private GameObject[] Cubes; //Assign how many ever cubes you have in the inspector

public void SetCubeActive(int ButtonInt)
{
    int CubeInt = ButtonInt - 1;
    for (int i = 0; i < Cubes.Length; i++)
    {
        if (i == CubeInt)
        { 
            Obj.SetActive(false);
            return;
        }
        Obj.SetActive(true);
    }
}

When you add this method to the Button script, you will get an int value you need to assign…
This int value is just the button you are working with (i.e. If you are currently doing this for the first button, then the int will be 1, etc…),
I think this should suit your needs :slight_smile: