So I'm trying to get unity to display a message upon destruction of a prefab that I have spawn into scene.
The script I wrote does not work when the prefab is spawned using the script (The while loop never stops, and the message does not print). However, if I were to instead have the object in the scene by default, the code runs fine and the message prints.
using UnityEngine;
using System.Collections;
public class TestDestroys : MonoBehaviour {
public GameObject enemy;
private bool trigger = false;
public HUDText hudText;
public Vector3 spawnLocation = new Vector3(10,4,10);
// Use this for initialization
void Start () {
StartCoroutine (test ());
}
// Update is called once per frame
IEnumerator test () {
Debug.Log ("Start");
Instantiate (enemy, spawnLocation, Quaternion.identity); //Spawn prefab into play.
while(trigger==false){ //Set variable trigger to "false" to loop while.
if (enemy == null) { //Test if prefab has been destroyed.
Debug.Log ("End");
hudText.Add("Welcome", Color.white, 3f); //If prefab is destroyed, print message.
trigger=true; //Set trigger to true to "true" to escape while loop.
yield return null; //Wait one frame
}
yield return null;
}
yield return null;
}
}