How to make a counter in GUI ?

Hey guys. I need help to make a count on GUI.Text

Each time that I shot in an Target it be destroyed and de count marks 10 points for example...

I have a code here but i dont know how to put the score in this code.

This make the targe be destroyed, how I do to count scores ?

function OnCollisionEnter (hit : Collision)
{
    if (hit.gameObject.tag == ("target"))
    {
        GameObject.Destroy(hit.gameObject); 
    }
}

5 Answers

5

http://answers.unity3d.com/questions/17985/pickup-count-on-gui

http://answers.unity3d.com/questions/33757/count-on-collision

http://answers.unity3d.com/questions/37604/counting-on-unity-iphone

these contain the information that you need to complete this task

I think the following will do,,

static var score : int = 0;

function OnCollisionEnter (hit : Collision){
    if (hit.gameObject.tag == ("target")){ 
       GameObject.Destroy(hit.gameObject);
       score += 10;   
    }
  }

if you want this score to be shown in a GUitext add the following script to the GUI

function OnGUI(){
   guiText.text = "Score: "+codeNameForDestructionAndScoreCountingHere.score;
}

M Poirot- What do I replace "codeNameForDestructionAndScoreCountingHere" with? I've tried several things but they don't work. The rest of your script is great. Thanks

You have to replace the “codeNameFor…” with the Script of the Collision ( where you write the line “score+=10;”

For Example:

function OnGUI(){
guiText.text = "Score: "+Controller.score;
}

how to make collosion and make counter gui between cube and fpc