- Home /
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
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;
}
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.
Are you sure you are passing an int?
Cause this compile error does not show on my unity.
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.
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.
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.
Your answer
Follow this Question
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