scoring sing guiText

i have now tried coding but the counter on the guiText score dose not work:

static var Counter : int = 0;

my other cube script

function OnCollisionEnter (myCollision : Collision)
{
   if(myCollision.gameObject.name == ("fireBall (Clone)"))
   {
      Counter++;  
   }
}
 my guiText script
function OnGUI(){
    guiText.text = "Score: "+CubeScript.Counter;
}

that’s better, did you delete your other question?

in the future please make sure you format your text using the 1010 button.

First of all, I would not really go the route of editing the variable directly from your collision object. but if you wanted to do that you need to tell it where Counter is.
your line Counter++; does not know where to go.
an easier way to go about it is to have this on your collider.

var guiController : GameObject;

function OnCollisionEnter(myCollision : Collision){
   if(myCollision.gameObject.name == ("fireBall(Clone)"))
   {
      guiController.BroadcastMessage("AddPoints",1);
    }
}

you drag your guiController game Object into the slot guiController so that is assigned.
then on your guiController you have this function

function AddPoints(incPoint : int){
    Score += incPoint;
}

that adds the score sent from your collider to the gui controller. You also don’t want to search for name in your gui controller its slower than searching for tag so you could use
myCollision.CompareTag(“fireball”); instead of myCollision.gameObject.name. You will have to assign the fireball prefab to have the tag fireball.

in gui you have the right idea, just change guiText.text = "Score: "+ Score; or change my variable score to counter.

I hope that gets you on the right track.

It looks fine and should work fine if the name is “fireBall (Clone)”

But if you do use OnCollisionEnter() then note it will only work on rigidbodys and static objects, but it will NOT work if you are using a trigger or a CharacterController.

  • So if you are using a
    characterController use
    OnControllerColliderHit().

  • If using a rigidbody use
    OnCollisionEnter().

  • if using triggers use
    OnTriggerEnter().