If (you find this) do this

I want to do something like if(gameObject.transform.Find(“Example”)) {do this}. Does unity consider this like “if you find “Example” do this”? In case not, what does this Find returns?

Find is a heavy, last resort thing to use. If you have a really simple setup it’s fine but it’s terrible at scale. It searches every object and compares string names until it gets a match.

You should either directly reference “Example” or register/cache it somewhere that it can be accessed.

1 Like

Transform.Find returns a Transform. To see whether Unity successfully found something, check whether the result is equal to null.

You can also store the result of Find() in a variable. This allows you to use the thing you found more than once without having to go find it again. You can test whether the variable is equal to null to see whether you found a thing, or whether the thing you found still exists.

2 Likes