x


Trouble sending message...

I'm trying to send a message from a coin prefab to a score gui to add 1 to the score everytime my player collides with a coin. Here's the coin script:

function OnTriggerEnter(other : Collider) {
if(other.gameObject.CompareTag("Player")){
other.gameObject.SendMessage("OnScore", null);
destroy = true;

}
}

Here's the score gui's script:

var score : int = 0;
function OnScore(){
score+=1;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), "SCORE: " + score);

}

Does anyone know what's wrong here?

more ▼

asked Oct 09 '10 at 07:32 PM

user-5369 (google) gravatar image

user-5369 (google)
1 1 1 1

The error reposrt is saying that OnScore has no receiver..

Oct 09 '10 at 09:07 PM user-5369 (google)

I am not able to run Unity where I am... is there a version of SendMessage that takes just a method name? I cant remember. It might be getting confused thinking it should be looking for a method with one argument (that happens to be set to null in your case). Try that or add a bogus float to the method. I'm pretty sure reflection is used to look up the method name.

Oct 09 '10 at 10:19 PM raypendergraph
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

SendMessage "calls the method named methodName on every MonoBehaviour in this game object."

OnScore() won't be found unless it's on the Player object, and by the sounds of things it isn't.

more ▼

answered Oct 10 '10 at 12:53 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

(comments are locked)
10|3000 characters needed characters left

The idea looks sound.

  • Make sure your player object has a collider and is assigned the tag "Player".
  • Make sure the coin collider is set to be a Trigger.

Use the debugger or put a Debug.Log line at the top of the OnTrigger function to make sure it is getting called. If it is, then the test for player tag is failing. If it isn't, then the problem is with getting the trigger to fire.

more ▼

answered Oct 09 '10 at 08:25 PM

Bampf gravatar image

Bampf
5k 8 19 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2485
x801
x83
x35
x15

asked: Oct 09 '10 at 07:32 PM

Seen: 817 times

Last Updated: Oct 09 '10 at 07:32 PM