- Home /
Instantiated object is destroyed in scene, but still logs as existing in console.
I am sure this is a simple beginner's error I am making, but I have tried everything I can think of and getting some help seems necessary now.
I have a ball which is spawned on Start (named 'Core'), and when that ball reaches the goal it is taken out of play and destroyed. This all works perfectly.
The Spawner is listening in update to see if the Core exists in the scene, and will Instantiate a new one if it is destroyed.
THE PROBLEM: The Core(clone) instance is destroyed in the scene, and it disappears as expected both in the game view and in the hierarchy of scene objects, but line 20-22 STILL logs out as the core existing in the scene. I just keep getting told it's still there somewhere, and so the new one won't spawn in it's place.
I have played with the SpawnCore() function to try and get it returning a GameObject, and trying to check for that, but it just tells me it's not accessible.
Hopefully it's an easy fix for someone, and if you could break down for me where I am going wrong it would help me learn a great deal and be very much appreciated!
Thanks!
public class BallSpawner : MonoBehaviour {
public GameObject corePrefab;
public float respawnTimer = 1.0f; //The delay in time in seconds until this ball respawns. Set to 0 for respawn the instant the ball is destroyed.
// Use this for initialization
void Start () {
SpawnCore ();
}
// Update is called once per frame
void Update () {
if(corePrefab){
Debug.Log ("CORE IN SCENE");
}
if(!corePrefab){
Debug.Log ("THERE IS NO CORE IN THE SCENE"); <----------- Never gets called.
Invoke ("SpawnCore", respawnTimer);
}
}
void SpawnCore(){
GameObject coreInstance = Instantiate (corePrefab, transform.position, Quaternion.identity) as GameObject;
}
}
Answer by Dragate · Oct 06, 2017 at 10:09 AM
In corePrefab you have assigned the prefab, which is always there. In Update() you are checking for the prefab, not the instance (coreInstance), which you should.
Make coreInstance known to the entire class and check for that in Update().
Thanks so much Dragate that worked. I hadn't fully understood the difference between the prefab and the instance.