Search Unity

How to disable and enable selected object using button.

Discussion in 'Scripting' started by Goldensnitch, Dec 29, 2016.

  1. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60

    hello.

    I have these 3d objects on my screen. I want to select any of this object and disable it with button. I am stuck. Please help me. This is the script so far i write. Thank you
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class disableEnable : MonoBehaviour {
    5.  
    6.     public GameObject Enable_Disable;
    7.  
    8.     public void Enable()
    9.     {
    10.         Enable_Disable.SetActive(true);
    11.     }
    12.     public void Disable()
    13.  
    14.     {
    15.         Enable_Disable.SetActive(false);
    16.     }
    17. }
     
    Last edited: Dec 29, 2016
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Look at the error in your console. You didn't see Enable_Disable to anything.
     
  3. MultiSpoone

    MultiSpoone

    Joined:
    Dec 7, 2014
    Posts:
    11
    The code is right. You just have to connect the buttons with the script. You can do that when you click on the button object and there is an "OnClick" Event or something like that. there you have to select the object the "disableEnable" script is on and then you can access it's public methods
     
  4. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Issue is solved thank you