- Home /
How to activate/deactivate object in if statements?
Hi, I am trying to make an object start invisible/ deactivated, and then after 4 seconds set it as active. I have the below code, but am very lost on how to make this work. Any help is much appreciated!!
void Start(){
gameObject.SetActive (false);
}
void Update(){
if (Time.deltaTime > 4s)
{
gameObject.active = true;
}
else if (Time.deltaTime < 4s)
{
gameObject.active = false;
}
}
Couple issues here,
Deactivating a GameObject (and therefore all Components attached to it) means that the Update() method will never get called (disabled $$anonymous$$onoBehaviour-> no Update() call). So you can't reactivate it that way.
Time.deltaTime is this (you need to read the documentation before using any Unity related API) probably will never get to 4f, otherwise you need very very slow framerate (
Lastly, it is bad practice to run an if block that always be true after some point, could cause unexpected behaviour and is bad for performance (trivial in this case thought).
So to solve this the direct way would be to use the Invoke method, like this
void Start()
{
gameObject.SetActive(false);
Invoke("Activate", 2f);
}
void Activate()
{
gameObject.SetActive(true);
}
Cheers!
this line will also deactivate all components attached to the gameobject (including this script):
gameObject.SetActive(false);
Well that's the point, deactivating the Behaviour doesn't mean that the Start method will stop immediately, it will finish normally and Invoke will be called.
try to have a parent object as a controller which will deactivate/activate this object preferably by some trigger event and not in Update() function
Answer by sisse008 · Jan 09, 2018 at 12:38 PM
change gameObject.active = false to gameObject.SetActive(false)
I just realized you are trying to deactivate the gameobject holding this script. you cant do that. once the gameobject is deactivated this script (or any other components attached to the game object) will not run anymore.
attach this script to an empty gameobject "controller" and pass the other gameobject by reference;
public GameObject _object;
private bool activatedObject = false;
void Start(){
_object.SetActive (false);
}
void Update(){
if ((Time.deltaTime > 4s) && !activatedObject)
{
_object.SetActive (true);
activatedObject = true;
}
}
Time.deltaTime will also never be larger than 4s. This is the delta time between frames. You need to have a separate float and do something like this:
private float delayTime;
void Update() {
delayTime += Time.deltaTime;
// your other code
}
Answer by Anonymous700 · Jan 09, 2018 at 02:04 PM
your time should also be in descending order .
GameObject.setActive(false);
GameObject.setActive(true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612150133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Condition not occuring in script 0 Answers
Screensaver when device is idle 1 Answer
How to subtract one time from another? 1 Answer
How often is the update function called ? 1 Answer
converting different time zones 2 Answers