Hi there,
I need to find some GameObject children by their name. This work perfectly fine if the child I am searching exists.
But if it does not exist, I get this exception:> NullReferenceException: Object> reference not set to an instance of an> object
According to the Unity docs, I would expect transform.find to return null instead of throwing an exception.
Any ideas?
GameObject productContainer; // parent Object that contains several (or null) children
GameObject item;
item = productContainer.transform.Find("product1").gameObject;
if (item == null)
// this code is never reached - if "product1" does not exist an exception is thrown
Debug.Log("Not found");
else
Debug.Log("Found"); // this code works if "product1" exists
Thanks in advance!
↧