GUI isn't displaying when collision occurs.

I’m trying to trigger a GUI when the bullet destroys the Virus object. I know the collision is being detected because the Debug Log is displaying “collided”, but the “msg = true” either isn’t triggering or the OnGui() function isn’t working.

var explode: Transform;
var msg = false;
var textArea = new Rect(0,0,Screen.width, Screen.height);
function Start()
    Destroy(gameObject, 2);

function OnCollisionEnter (collision: Collision) {
	Instantiate(explode,transform.position, transform.rotation);
	Destroy (gameObject);
	if( == "Virus")
		msg = true;

function OnGUI()
		GUI.Box (textArea, "Test Sentence");

Scripts cannot run on a destroyed GameObject, and cease execution immediately when destroy is called

  • Nothing will run two seconds after start
  • Nothing will run after line 11 executes, including setting msg = true

You have two options to solve

  1. Don’t call destroy, instead disable components on the gameObject. Call destroy when everything is finished.
  2. Call a method from another script not on this gameObject before calling destroy. Have the other script control the message function. (Don’t call a coroutine, coroutines also stop when the calling object is destroyed)