Search Unity

Reactivate Game Object

Discussion in 'Scripting' started by Vindatra14, Aug 4, 2015.

  1. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    I have this button to disable my gameobject, but I want to activate it again when I click on another button. When I write the same code GameObject.Find ("body maker").SetActive (true) it can't activate back and still disable. How can I make this button enable again.

    Code (CSharp):
    1. public class TakePhoto : MonoBehaviour{  
    2.     void OnMouseDown()
    3.     {
    4.         GameObject.Find ("body maker").SetActive (false);
    5.     }
    6. }
     
  2. Daso1970

    Daso1970

    Joined:
    Aug 3, 2015
    Posts:
    9
    The problem is probably that once you deactivate the GameObject it will stop receiving messages such as OnMouseDown.

    You need to either move the deactivation/activation code somewhere else or instead of using SetActive you set a variable to true/false upon OnMouseDown which is checked everywhere in your components for that GameObject to determine if it is active or not.
     
  3. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    hhmm I mean like this I have button a b and c. First onmousedown I disable button c, but I want to make if I onmousedown button b that button c become active again, something like that
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You need to save the button reference to a class variable on Awake or Start, for both scripts (you can use Find while it's still enabled). Alternatively, you can make public class members on both and just drag the reference into it in the inspector (then whether it starts enabled or not is irrelevant.

    You can re-enable GameObjects easily as long as you have a reference to them, but you can't use Find or FindWithTag to locate disabled GameObjects, so you need to have the reference beforehand. Get it?
     
  5. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    hhmm excuse me Lysander I understand what u mean, but I'm still new at unity and still learn it. How I reference that GameObject can u write some script or code with explanation ?
     
  6. Daso1970

    Daso1970

    Joined:
    Aug 3, 2015
    Posts:
    9
    hhmm he means that GameObject.Find ("body maker") won't find "body maker" if it is disabled.

    So put variable = GameObject.Find ("body maker") into a Start method so you will have a reference to it. Then you perform variable.SetActive (false); instead of using Find in your OnMouseDown methods.

    Code (CSharp):
    1. public class TakePhoto : MonoBehaviour{
    2.   GameObject variable;
    3.  
    4.   void Start()
    5.   {
    6.   variable = GameObject.Find ("body maker");
    7.   }
    8.    
    9.   void OnMouseDown()
    10.   {
    11.   variable.SetActive (false);
    12.   }
    13. }
     
  7. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    How about like this I have 3 child gameobject a b and c and my parent is GameObject. So GameObject include a b and c, then I want to make button a to disable button c and button b to disable button b, How I check if that button is click ??
     
  8. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    any1 can help me with this, I'm gonna start some conversation if any1 want to help me. Maybe many question because still new to Unity