How to disable selected object using button?

Hello,

First of all greetings to classmates. I trying to make 3D block breaker game but i stuck with this problem. I have these objects on my screen and i want to select it and disable it with button. So far script that wrote isn’t that i wanted cause it only disable object i attach to it. But i have 65 children attach to one parent. Please help me.

using UnityEngine;
using System.Collections;

public class Disabler : MonoBehaviour
{

public GameObject Enable_Disable;

public void Enable()
{
    Enable_Disable.SetActive(true);
}
public void Disable()

{
    Enable_Disable.SetActive(false);
}

}

I’m not sure I understood what you were trying to say.

Do you want to disable/enable all objects which are children of a specific game object? If that’s the case, you need to to put the parent object in the field Enable_Disable of the Disabler script component in the Inspector.

BTW, I don’t even understand your screenshot: did you put all the objects (cubes, sphere, cylinder) inside the Canvas parent? Probably that’s the mistake, try creating a new parent object which contains only the cubes etc., and leave inside the Canvas parent only the buttons, so you can disable via buttons only the cubes parent without disabling the Canvas itself (which would disable the buttons too).

Sorry Galandil. I just want to disable selected object using disable button. If i select one of these object and click on the disable button object should be hide. But script that i wrote only hide object that i attach to it.

So i want this button disable objects that i selected. If i put parent object in the field Enable_Disable of the Disabler script component in the Inspector it disables all the objects.

using UnityEngine;
using System.Collections;

public class Disabler : MonoBehaviour {

public GameObject Enable_Disable;

if (Input.GetMouseButtonDown(0))
{
print(“click”);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit,100.0f))
{
Enable_Disable = hit.collider.gameObject;
Disable()
}
public void Enable()
{
Enable_Disable.SetActive(true);
}
public void Disable()

{
Enable_Disable.SetActive(false);
}
}

Ok, now I got what you’re trying to do. :smiley:

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).

Some words as to why your code didn’t work. It all has to do with how the OnClick() event of the buttons works.

You need to feed it a game object in order to then call a method of a script component of the object linked to the event.

You were trying to put the disable/enable methods inside the script with the trigger event. This means that, inside the inspector, you had to feed a specific game object (let’s say, a cube instead of a sphere or a cylinder), so the enable/disable methods would work only for the game object you linked to the OnClick() event.

So you have to “decouple” the trigger event (i.e.: the OnMouseDown method) from the disable/enable methods, putting them in different scripts, which have to be components of different objects. This way, the object that has the script with the enable/disable method can be linked to the OnClick event and will work regardless of which object you triggered by the mouse click.

Hope this helps.

You are savior THank you so much. I have error on this script

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;
}

}

What error?

Assets/Scene/Scripts/TriggerScript.cs(6,13): error CS0246: The type or namespace name `Disabler’ could not be found. Are you missing a using directive or an assembly reference?

Thank you

Check if the script filenames are the same as the name of the class, i.e.: Disabler.cs and TriggerScript.cs

Galandii Thank you so much I don’t know what to say. For last thing how do i make this object disabled forever?

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).

You already told me about it. But how do i make it remain disable forever?

You can add a third button and a new method in the Disabler script, which just destroys the object, or you can simply remove these lines:

if (gameObject != disabler.selectedObject){ disabler.Enable(); }

from the OnMouseDown method of the TriggerScript.

Galandil Thank you so much again This is all i want.

1 Like

Privacy & Terms