Changing the GUIText on Collision

Hey everyone,

I was just wondering how I would go about changing GUIText when an object is hit. Like
having 3 separate GUI texts and when the game object is hit the text will change to another.
For Example I have an enemy that gets hit 3 times before being destroyed I want “X3” to show next to him then when hit it says “X2” and so on until it is destroyed. I was thinking of using a OnTrigger event then when the object is hit call the update to change the GUI Text.

I think I’m on the right track I just need some guidance on what would be a better method or how to go about doing it the way I explained.

If someone could help that would be great.

First, you need the references of the GUITexts in the script of your enemy. Then, depending on the collider type (trigger or not) override OnTriggerEnter or OnCollisionEnter. In those function, you’ll need to change the guiText1.text and so on. Judging by your description, you’re going to need a counter too (3, 2, 1 …).

That might help : http://unity3d.com/support/documentation/ScriptReference/GUIText.html

you can try to use this script:

var score = 3f;



function OnControllerColliderHit (hit : ControllerColliderHit){

  if(hit.gameObject.tag == "enemy")

  {

     score = score-1;

  }

}

function Update (){

GameObject.Find("GUI Text").guiText.text = "x"+score;

}

Or this script:

var hitpoint = 3f;

function OnControllerColliderHit (hit : ControllerColliderHit){

if(hit.gameObject.tag == “enemy”)

{

 hitpoint = hitpoint-1;

}

}

function Update (){

if(hitpoint == 3){

GameObject.Find(“GUI Text”).guiText.text = “x3”;

}

if(hitpoint == 2){

GameObject.Find(“GUI Text”).guiText.text = “x2”;

}

if(hitpoint == 1){

GameObject.Find(“GUI Text”).guiText.text = “x1”;

}

}

Both scripts works…all you have to do is to add one of those scripts to your player or bullet.