- Home /
SetActive(true) not working
It seems that I am not able to reactivate the game object once I deactivate it. Is there a workaround?
void Update()
{
if(Vector3.Distance(Player.transform.position, transform.position) <= 18f)
{
gameObject.SetActive(true);
}
if(Vector3.Distance(Player.transform.position, transform.position) >= 18.1f)
{
gameObject.SetActive(false);
}
}
Answer by KiraSensei · May 01, 2014 at 08:54 PM
Since an inactive game object has all its components inactive, the script attached to it does not run anymore. This question was asked a lot of times, and answered a lot of times.
For example this answer will help you.
But I had been using the exact same code for all my levels and it always worked (it still is). But it's only not working this one specific time.
That's not possible. When the gameobject itself is deactivated, its Update method will no longer run. A gameobject can turn itself off inside Update but it can't reverse that. That should be pretty obvious. Of course a gameobject can turn a different gameobject on or off as long as the gameobject that the script is attached to is still enabled and active.
Please don't necro-post on such ancient questions. If you have a specific question with your specific code, please ask your own, seperate question. This question was clear and has been answered already. UA is not a forum but a Q&A site.
Answer by IAmChiagozie · Jul 07, 2019 at 07:19 PM
Attach the script to a different game object on the hierarchy, then create a new variable of type game object: public GameObject theObject
Then set theObject.SetActive(false);
And theObject.SetActive(true);
Your answer
Follow this Question
Related Questions
gameObject.find doesn't work 1 Answer
activate GameObject dont work 1 Answer
GameObject.setActive(bool) is not activating my gameObject 1 Answer