• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user-5369 (google) · Oct 09, 2010 at 07:32 PM · collisionplayermessagecoinsend

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-5369 (google) · Oct 09, 2010 at 09:07 PM 0
Share

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

avatar image Ray-Pendergraph · Oct 09, 2010 at 10:19 PM 0
Share

I am not able to run Unity where I am... is there a version of Send$$anonymous$$essage 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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Marowi · Oct 10, 2010 at 12:53 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bampf · Oct 09, 2010 at 08:25 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Collision script 1 Answer

Player Character Health 2 Answers

How to turn of collision for the player 1 Answer

Only targeting the targets root of vector 3 and not colliding with its mesh 1 Answer

Picking up a coin in 2D 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges