score not updating

The basic concept is to collect objects, each time it collects a coin it increases the score by 5 and each time it collects the star it increases the score by 10.

I also have a collision script:

function OnTriggerEnter(other:Collider)
{
	SendMessageUpwards("addScore");
	Destroy(...);
}

I have a control Script:

public var score = 0;

         
   function collect()
	{
.....

    }
    
function addScore()
{
	if (tag == "coin") {
       score = score + 5;
     }
    
   else if (tag == "star") {
    score = score + 10;
    }
    updateScore();	
}

function updateScore()
{
	.....
}

The score does not update when collected?

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.