Updating score when colliding

Hey everyone,

Im an artist trying to code and basically I’m crap,

Im trying to get a simple score mechanism where when the player collides with an object it updates the score. I’ve been around the forum trying to find ways other people have done it and most seem to have two scripts one for the score controller and one for the object getting hit. I have no idea how to link them and how to get the hit script to update the scorecontroller script, I would need to update it say 50 times for all different types of score hits.

here are my two scripts:

ScoreController:

var score : int = 0;

function OnGUI () {

GUILayout.BeginArea ( Rect ( 10, 10, Screen.width / 4, Screen.height / 4 ) );
GUILayout.Box ( score.ToString () );
GUILayout.EndArea ();

}

which displays the score on screen and

Hit :

function OnTriggerEnter( hit : Collider)
{

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

{

Destroy(gameObject);

}

}

which all it does is destroy the object on collision

any help would be awesome

SendMessage()

could you elaborate maybe show it in the context of my script im not sure how that works in relation to what i have already

SendMessage allows you to invoke functions found in scripts attached to other game objects.
In order to use SendMessage the script that you want to make the call from (Your Hit Script) will need to know the gameobject to which the message should be sent and the function that needs to be invoked in order to update the score.

First to you scorecontroller, add a function called IncrementScore with a parameter called Value of type int

Within you Hit script you could optionally add a variable called PointsToAward as type int.
Update the onTriggerEnter script to do 2 things before you destroy…

  1. Locate the ScoreController
  2. Invoke the IncrementScore function passing the PointsToAward as the Value.

I won’t give you the code for this but you can find how to do each in the script reference

  1. GameObject.Find
  2. GameObject.SendMessage

Hope this helps…