This is a weird situation for me... when disabling slide.gameObject.SetActive(false) works perfectly, but when i need to enable it doesn't work

the strange part is that debug is correctly showing the gameobject name

Can you help me?

public void ShowSlide(Transform slide, bool show, float time){if (show){// ShowDebug.Log(slide.gameObject.name);slide.gameObject.SetActive(true);// slide.Find("Panel").DOLocalRotate(new Vector3(0, 0, 0), time);}else{// Hideslide.Find("Panel").DOLocalRotate(new Vector3(90.0F, 0, 0), time).OnComplete(()=>slide.gameObject.SetActive(false));}}
2

Best Answer


Where do you have the script attached to?. Cause you can't enable an object from a script wich is attached as a component of the same object. This makes sense if you think that first you disabled the object, so the script is disabled as well as the object. That means that you can disabled it but you could not enabled it again. For this you need to attach this script to another object in order to work, like an empty parent.

The possible reason is that you disable the gameObject in the Awake() method - so when you are trying to activate it later, Awake() is invoked, and the gameObject gets deactivated again. The docs say that Awake() is invoked every time when inactive GO becomes active again : "Awake is called either when an active GameObject that contains the script is initialized when a Scene loads, or when a previously inactive GameObject is set to active"