Ok, now I got what you’re trying to do.
You’ll need two different scripts, a Disabler script and a TriggerScript.
Do the following:
- Create a parent game object (let’s call it Container), which will have all the children objects inside (all the cubes, spheres, etc.)
- Create all the children objects inside Container that you want
- Attach the Disabler script to the Container object
- Attach the TriggerScript to all the children objects
- The children must have a Collider component, and with IsTrigger set to true
Now, the code of the two scripts.
Disabler.cs:
[code]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Disabler : MonoBehaviour {
public GameObject selectedObject;
public void Disable(){
if (selectedObject != null) selectedObject.SetActive(false);
}
public void Enable(){
if (selectedObject != null) selectedObject.SetActive(true);
}
}[/code]
TriggerScript.cs:
[code]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerScript : MonoBehaviour {
private Disabler disabler;
void Start(){
disabler = GameObject.FindObjectOfType<Disabler>();
}
void OnMouseDown(){
if (gameObject != disabler.selectedObject){
disabler.Enable();
}
disabler.selectedObject = gameObject;
}
}[/code]
And, finally, go to the buttons and on the OnClick() event give the Container game object, and select the appropriate method from the menu.
This code will not only allow you to disable/enable the last selected object at runtime, but will automatically enable back the previous object if it’s disabled and you select a new object (in order to avoid the last one to remain disabled forever).