Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
    }
}
share|improve this question
2  
Could you explain what your code does and what you expect it to do? –  Anko Aug 6 at 15:40
1  
To follow up on that request, note that all you say is "my script does not work". Please be more specific about what happens. –  jhocking Aug 6 at 21:36
    
I have updated my question. Sorry and thanks. –  user4985 Aug 7 at 2:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.