- Home /
How do I re-instantiate original prefab after a game object inside has been destroyed?
I am making an infinite runner. My player collects tokens (at which point they are destroyed), and once he collects a red token, I re-instantiate the level to create the illusion of an infinite level. My problem is that when I re-instantiate the level, my tokens are not showing up because they have already been destroyed. Does anyone know how I can make it so that my level prefab shows back instantiates with all the tokens back in place? Everything else on my level gets re-instantiated except for objects that have been deleted. Bellow is my trigger where everything gets done.
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.name.Contains ("Player"))
{
if(count == 0){
Debug.Log("Enemy Collision");
Instantiate(ground,new Vector3(distance, 0, 0), Quaternion.identity);
Destroy(ground);
distance = distance+ 40.31f;
count=1;
}
else{
Debug.Log("Enemy Collision");
Instantiate(ground,new Vector3(distance, 0, 0), Quaternion.identity);
Destroy(ground);
distance = distance + 40.31f;
}
}
}
Answer by Denvery · Mar 15, 2015 at 10:10 AM
Can you try not to destroy your tokens, but make them inactive?
game object.SetActive(false)
And activate them when reactivating level? May be it helps...
Answer by JedBeryll · Mar 15, 2015 at 07:23 PM
You could try destroying the instance.
GameObject newGround = Instantiate(ground,new Vector3(distance, 0, 0), Quaternion.identity) as GameObject;
Destroy(newGround);
This way you keep the original ground.