- Home /
how to get game object by string?
Hi, I got 2 string arrays which contains names of gameobjects. Using foreach loop I'm going through each element in 2 arrays and comparing values. If values matching each other then I need to get gameobject with that value and change it's parent, but here i got a problem, because GameObject.Find(b) - returns Null, but Debug.Log(b) - returns the correct value. Please Help. Sorry for my English
foreach (string a in splitSkillSlots)
{
foreach (string b in splitaNames)
{
if (a == b)
{
Debug.Log(b);
GameObject.Find(b).transform.SetParent(skillBar.transform);
}
}
}
Comment
if a matches b that does not mean there is a gameobject with that name in scene. what you need to do is check if object exist by null check.
Debug.Log(b);
GameObject g = GameObject.Find(b)
if( g != null ){
g.transform.SetParent(skillBar.transform);
}