- Home /
MissingComponentException: There is no 'GameObject' attached
So I've been creating a GameObject pool so that I can 'spawn' a lot of objects quickly, and also to recycle them instead of destroying them.
There are two classes that handle this, one for my 'StateObject', which is my extension of GameObject that runs on a state machine. This StateObjectManager is done, working, and everything is fine.
Now, I cloned this StateObjectManager and set it up for regular GameObjects, but when I try to bring GameObjects from the pool into the game using my SpawnToGame() method, I get the following:
MissingComponentException: There is no 'GameObject' attached to the "New Sprite(Clone)" game object, but a script is trying to access it.
You probably need to add a GameObject to the game object "New Sprite(Clone)". Or your script needs to check if the component is attached before using it.
Now of course this doesn't make much sense - as GameObject can't not be there? can it? I've tried creating new objects to see if that was the problem but it still occurs. Here's the SpawnToGame() method:
public List<GameObject> SpawnToGame(string tag, Vector3 pos, Quaternion rot, System.Int16 amount = 1) {
if (!objects.ContainsKey(tag)) {
Debug.LogError("There were no instances of " + tag + " in the pool, spawn some to the pool first.", this);
return null;
}
List<GameObject> toSpawn = objects[tag].Where(e => !e.activeInHierarchy).Take(amount).ToList();
if (toSpawn.Count < amount)
Debug.LogError("There was an insufficient ammount (" + toSpawn.Count + ") of " + tag + " in the pool to spawn " + amount + " of them.", this);
foreach (GameObject go in toSpawn)
TurnOn(go, pos, rot);
return toSpawn;
}
The line in question is where I create the toSpawn List. Keep in mind I have the exact same method that works absolutely fine for my StateObjects, the only difference being this, 'e.gameObject.activeInHierarchy' instead of 'e.activeInHierarchy'
If this were any other component (an Animator for example, then the problem would be obvious - I'm not aware that objects in unity can exist without having a gameObject?)
Your answer
![](https://koobas.hobune.stream/wayback/20220611231737im_/https://answers.unity.com/themes/thub/images/avi.jpg)