I have a GameObject named “CharacterCatalog”. It’s children are GameObject prefabs that I tag (“Player”) to represent Characters. The code I have below used to work until I disabled a Mesh Renderer within my characters. The Instantiate call is now returning null as if my Prefabs are corrupted or something. Any ideas?
I get an array of these characters with this:
maAvatarGameObjects = GameObject.FindGameObjectsWithTag("Player");
And I Instantiate a copy of the character with this:
public GameObject CreateAvatar (string sCurrentAvatar)
{
foreach (GameObject go in maAvatarGameObjects)
{
Debug.Log("Comparing " + go.name + " to " + sCurrentAvatar);
if (go.name.Equals(sCurrentAvatar))
return (GameObject)Instantiate(go);
}
return null;
}
I have more information:
If I create a public variable in my script and drag a prefab onto that variable, it looks fine in the inspector. When I hit play, it disappears from that variable. If I set the value from the dropdown while playing, it’s able to instantiate. If I set the value from the dropdown while NOT playing, I get the same problem (it’s empty when playing, and of course, Instantiate returns null).
By the way, if I dump my array from FindGameObjectsWithTag, they are all valid GameObject objects.
Something is fishy about this.
I solved the problem by:
- Deleting the prefabs in the project (but not the ones in my Scene).
- Created new empty prefabs in my project.
- Dragged the instances in my Scene back onto the blank prefabs.
It seems that I was right that the prefabs were corrupted because the instances in my Scene were the same objects essentially. In other words, I did not change the settings of any of the Scene objects.
I had the same problem, here you are the solution ^^