Hello everyone
I’m trying to store instantiated clone inside a list of gameObjects. When I try to store it, I actually store the clone’s prefab.
See my code below :
public GameObject objectPrefab; <-- I fill the prefab in my inspector here
public List<GameObject> listOfClones = new List<GameObject>(); <-- Hidden in inspector
Awake()
{
GameObject newObjectClone = Instantiate(objectPrefab, transform.position, Quaternion.identity);
listOfClones.Add(newObjectClone); <-- Stores a prefab, not a clone.
}
How can I store clone instead of clone prefab ?
You’re probably falling afoul of a misconception here. The code you posted will store the reference to the instantiated object in listOfClones, not the prefab. There cannot be any second guesses about this, unless some other code is also modifying listOfClones.
So, how did you determine that the listOfClones contains prefabs, not instances?
You could log the GetInstanceID()
and you will notice the instance’s number is not the same as that of the prefab.
By all intents and purposes, they are identical except for their name. Instances by default are named the same as the prefab but with ‘(Clone)’ suffix.
1 Like
Hello CodeSmile,
Thank you for your response. It confirmed my suspicion that the problem came from elsewhere.
After some research, it turns out that this is indeed the case.