How to disable unselected objects using button?

Hello,

I have been working on this ZombieRunner game but i am stuck with this problem. " I have these 3d spheres, button and i want this button to disable the unselected objects. How do i do that? Please help me. Thank you

Hello there, you can initialize a bool named “TurnOff” as “true” to each gameObject that you want this behaviour to happen, and then you make that when you select a gameObject, the TurnOff value will be changed in this gameobject to false.
After that you can itinerate through all gameobjects using a foreach and disable all the ones with TurnOff returning “true” whenever you click in the disable unselected.

I don’t understand what you are saying sorry

I edited my last reply to make it clearer

Quicker way to make this work:

  • Create a Container game object, with a Container.cs script in it
  • Create all the child objects (spheres) inside the Container, in each of the child create a Sphere.cs script, and remember that these objects need a collider with the IsTrigger active
  • Create two buttons, Disable and Enable

The scripts are as follows.

Container.cs:

[code]public class Container : MonoBehaviour {

public Transform sphereSelected = null;

    public void Disable(){
        foreach (Transform sphere in transform){
            if(sphere != sphereSelected) sphere.gameObject.SetActive(false);
        }
    }

    public void Enable(){
        foreach (Transform sphere in transform){
            sphere.gameObject.SetActive(true);
        }
    }

}[/code]

Sphere.cs:

[code]public class Sphere : MonoBehaviour {

Container container;

    void Start () {
        container = GetComponentInParent<Container>();
    }

    void OnMouseDown(){
        container.sphereSelected = transform;
    }

}[/code]

Feed to the buttons OnClick event the Container game object, in the disable one choose of course the Disable() method, in the enable one Enable(), and that’s it.

1 Like

Galandil you are life savior lol thank you

Very nice solution over there :astonished:

Joao_Dalvi Thank you for you too

Hello Galandil

What happens if i want to disable object with same tag name “Block”

Privacy & Terms