Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 MachCUBED · Apr 13, 2012 at 11:59 PM · javascriptfunctionfunctions

Issues using NotificationCenter to pass value from one object to another

Hi guys,

I'm using NotificationCenter as per the wiki in my game. In it, I have GemPickup.js for gems that get picked up.

 ...
 NotificationCenter.DefaultCenter().PostNotification(this, "AddScore", scoreValue);
 ...

LevelStatus.js manages all the score code, with the member variable "score : int" used for the score.

 ...
 NotificationCenter.DefaultCenter().AddObserver(this, "AddScore");
 ...

The above code is in the Start() function of LevelStatus.js, and the implementation of AddScore() is as follows:


function AddScore(addScore : int)
{
    score += addScore;
}

When the notification is posted, I get the following error in the console:


MissingMethodException: The best match for method AddScore has some invalid parameter.
System.MonoType.InvokeMember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters)
UnityEngine.SetupCoroutine.InvokeMember (System.Object behaviour, System.String name, System.Object variable)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
LevelStatus:LevelStatus$SendMessage$System.String$Notification$UnityEngine.SendMessageOptions(Object, Object[])
UnityScript.Lang.UnityRuntimeServices:Invoke(Object, String, Object[], Type)
NotificationCenter:PostNotification(Notification) (at Assets/Game Logic/NotificationCenter.js:79)
NotificationCenter:PostNotification(Object, String, Object) (at Assets/Game Logic/NotificationCenter.js:61)
GemPickup:OnTriggerEnter(Collider) (at Assets/Props/Scripts/GemPickup.js:26)

Am I using the right approach to pass variables between objects?

MachCUBED

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GuyTidhar · Apr 14, 2012 at 07:32 AM

I think you need to do duck typing (try not to define the type of addScore)

 function AddScore(notification: Notification) 
 { 
    score += notification.data; 
 }
Comment
Add comment · Show 8 · 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 MachCUBED · Apr 14, 2012 at 03:54 PM 0
Share

If I don't type it (s per the code you posted), I get the following console error if I try to compile it:


Assets/Game Logic/LevelStatus.js(61,15): BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'.

If I type it as per my original code, the error specified in the original post is a runtime one.

avatar image GuyTidhar · Apr 14, 2012 at 05:43 PM 0
Share

Are you sure you are passing an int?

Cause this compile error does not show on my unity.

avatar image MachCUBED · Apr 14, 2012 at 05:48 PM 0
Share

Yes, I'm passing an int. Here is GemPickup.js in all of its entirety, for the sake of reference:

#pragma strict

var scoreValue : int = 1;

private var target : GameObject;

private var pickedUp = false;

private var level$$anonymous$$anager : GameObject;

var pickupSound : AudioClip;

function Start () { target = GameObject.FindGameObjectWithTag("Player"); level$$anonymous$$anager = GameObject.FindGameObjectWithTag("Level$$anonymous$$anager"); }

function OnTriggerEnter (other : Collider) { // $$anonymous$$ake it go towards the penguin and disappear pickedUp = true; Debug.Log("Gem collected!");

 Debug.Log("Add score");
 
 //TODO: Add scoring system
 NotificationCenter.DefaultCenter().PostNotification(this, "AddScore", scoreValue);
         
     AudioSource.PlayClipAtPoint(pickupSound, transform.position);
     Destroy(gameObject);

}

scoreValue is an int. It is also the value being passed through to the notification center, as seen in this line:


NotificationCenter.DefaultCenter().PostNotification(this, "AddScore", scoreValue);

Although the documentation for the notification center says any data object can be passed to it, I don't think that's a problem, since the second argument taken by PostNotification() is untyped.

avatar image GuyTidhar · Apr 14, 2012 at 05:50 PM 1
Share

According to the notification center documentation you should do this:

function AddScore(notification: Notification) { score += notification.data; }

where data is the value of what you have passed to the observer.

avatar image MachCUBED · Apr 14, 2012 at 06:02 PM 0
Share

It worked, but only after I turned off #pragma strict in LevelStatus.js. Unfortunately, turning off #pragma strict reduced my framerate by maybe 20%. If I try to compile your last solution with #pragma strict uncommented I get:

Assets/Game Logic/LevelStatus.js(61,15): BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'.

This means that for #pragma strict to work, I need to have it check to see if notification.data is of the datatype int, and bail if it isn't.

Show more comments

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Nested Functions 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to use a function from another script inside of the Invoke () class? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How do I call a script or a function to be active 'while'a GUITexture is displayed? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges