Search Unity

GameObject.SetActive will disable but not enable child game object

Discussion in '2D' started by samurai_sam, Jun 12, 2019.

  1. samurai_sam

    samurai_sam

    Joined:
    Jun 5, 2019
    Posts:
    3
    The title pretty much says it all. I can enable and disable objects that are not a child of anything just fine but as soon as I make said game object a child it will disable but will not re-enable. I am a complete beginner in the world of Unity and programming (been doing it for about 2 days now) so explain things to me like I'm stupid please! Thanks!

    -Nick

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HideWheel : MonoBehaviour
    6. {
    7.  
    8. public GameObject front;
    9. public GameObject rear;
    10.  
    11. public bool Equip;
    12.  
    13. private void Update()
    14. {
    15.  
    16.  
    17.  
    18.  
    19. //activate front tire
    20. if (Equip == true)
    21. {
    22. front.SetActive(true);
    23. }
    24. else
    25. {
    26. front.SetActive(false);
    27. }
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34. //activate rear tire
    35. if (Equip==true)
    36. {
    37. rear.SetActive(true);
    38. }
    39. else
    40. {
    41. rear.SetActive(false);
    42. }
    43.  
    44. }
    45.  
    46.  
    47. }
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    Depends where you script is. If your script is part of the object being disabled, then the script is disabled (and hence Enable will not be called). Try moving the script to the root layer or somewhere else and see if it helps.
     
    breylinlee and kaancetinkayasf like this.