Here is my code:
GameObject no=GameObject.Find("LevelButton");
GameObject noparent=GameObject.Find("LevelPHwithrect (" + (i + 1) + ")");
The problem is that I am able to find the gameobjects
, but when I call
if(no.transform.parent.name==noparent.transform.name)
{
//do something
}
I get an error saying null reference, even when it's finding the object and I have set parent of no to noparent.
Please forgive me if there are errors in this post, as this is my first day on stack overflow.
Use the name
property of the GameObject
, not Transform
. Also code defensively and check for null
:
if (no != null && noparent != null && no.name == noparent.name)
{
//do something
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments