Hang on, I need to understand your setup.
So collisionDetector.js is on the spawned object (coin or star)
the coin/star is a child of the object with the spawnControl.js
So SendMessageUpwards is working.
Now … how is the spawnControl supposed to know what child the message came from?
if (coin) // you say works, yes but not for the reasons you think
this means if ( coin : GameObject is not equal to null ) , that’s why it functions, but it is wrong.
Look at the parameters that can be used in the SendMessageUpwards command :
SendMessageUpwards (methodName : String, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver)
So, you can send a value when calling a function . Try :
function OnTriggerEnter(other:Collider)
{
Debug.Log( gameobject.tag + " is sending a message to the spawnScript" );
SendMessageUpwards("addScore", gameObject.tag);
Destroy(gameObject);
}
then :
function addScore( theTag : String )
{
Debug.Log( "A message was just received by " + theTag );
if (theTag == "coin") {
score += 5;
updateScore();
}
else if (theTag == "star") {
score += 10;
updateScore();
}
}
Do you understand what is happening here?
This is using SendMessage to send a value to the called function. The function gets that value as a String, and then checks what that value is.