If one is trying to cache a child game object for later deactivation, what is the most efficient method?
var myGameObject : GameObject
function Start () {
// Which is faster:
myGameObject = GameObject.Find("MyGameObject");
// Or:
var myTransform = transform.Find ("MyGameObject");
myGameObject = myTransform.gameObject;
}
function DisableNow () {
myGameObject.SetActiveRecursively (false);
}
Or is there another way?
You’re only doing it once, so it’s irrelevant. You should use whatever method is easiest most appropriate for the situation. Although your second method would be less code if you did
myGameObject = transform.Find ("MyGameObject").gameObject;
–Eric
As eric mentioned, transform is like 10x times faster than GameObject.
I didn’t mention that.
I don’t really know about the speed of Transform.Find vs. GameObject.Find. I did say it’s irrelevant though, since you’re only doing it once.
–Eric
Mmmmm.
I kindov knew that transform.Find was faster than GameObject.Find, but I guess I was curious how fast Transform.gameObject was.
I was assuming transform.Find (“MyGameObject”).gameObject; was faster, as it already knew where to look for the GameObject once it had the reference to the Transform, and (iirc) transform.Find is restricted to the children of the GameObject holding the Monobehaviour - but there are times when -dangit - I just want to be sure.
Mainly this is because I’m still targeting limited devices like the iPhone and shaving anything during start makes a big difference.
(Oddly people seem to be much more patient with watching a progress bar when starting a web browser game, than one on their phone…)