Z
Z
Zefirot2021-11-07 22:14:59
Unity
Zefirot, 2021-11-07 22:14:59

Is there any point in checking activeInHierarchy?

I often have this piece in my code

if(obj.gameObject.activeInHierarchy){ obj.gameObject.SetActive(false); }
....
if(!obj.gameObject.activeInHierarchy){ obj.gameObject.SetActive(true); }

Is there any point in such checks or just leave without them?
obj.gameObject.SetActive(false);
....
obj.gameObject.SetActive(true);

I just don’t know what will be faster in general - checking and if it doesn’t match, then the state changes, or whether the state changes immediately, even if it already exists ....
And I have a lot of such checks ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ente, 2021-11-08
@Zefirot

There is one good rule for any popular frameworks: don't try to be smarter than them. Unity does not slow down in speed if you turn on an enabled object or turn off a disabled one, all checks are already enabled inside these functions. And in the examples above, only clogging the code with unnecessary checks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question