- Home /
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?
Step 1 : read this page : http://answers.unity3d.com/page/newuser.html
Duplicate Question : http://answers.unity3d.com/questions/434175/how-to-access-a-function-from-another-script.html
Please Delete this Question.
Please be patient if your question/reply doesn't show straight away.
As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it.
http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
sorry the question is asking something else now as the first one was changed slighty and that was about accessing the funciton but this is about the score
Ok, I shall give the benefit of the doubt.
Your problem probably lies here :
if (tag == "coin")
do you mean
if ( gameObject.tag == "coin" )
Also do some debugging. Are you sure it isn't the same problem? Is the function actually being called?
function addScore()
{
Debug.Log( gameObject.name + " just had addScore called" );
if ( gameObject.tag == "coin" ) {
// ....
Answer by AlucardJay · Apr 08, 2013 at 07:12 PM
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.
Ok so this is where using Debug really shines. I have updated my answer, but here are the changes using Debug.Log :
function OnTriggerEnter(other:Collider)
{
Debug.Log( gameobject.tag + " is sending a message to the spawnScript" );
Send$$anonymous$$essageUpwards("addScore", gameObject.tag);
Destroy(gameObject);
}
then :
function addScore( theTag : String )
{
Debug.Log( "A message was just received by " + theTag );
if (theTag == "coin") {
score += 5;
}
else if (theTag == "star") {
score += 10;
}
updateScore();
}
Check your console when you collect an item. What is the gameObjects tag that is sending the message, and what is the spawnScript saying it is hearing?
I think you have your tags not set up, and because you are only checking for a tag, and even if the tag is not coin or star, you are still calling updateScore();
try :
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();
}
}
now updateScore(); shall only be called when the tag matches on of the conditional checks. Definitely start using Debug, it shows exactly where you are in the script, and what is happening.
Actually, that doesn't matter I just realized, updateScore(); is only modifying the GUI text. sorry (it is very late for me). But still, debug and see what is happening.
Your spawnControl script, to what object is it attached?
Your answer
![](https://koobas.hobune.stream/wayback/20220613095931im_/https://answers.unity.com/themes/thub/images/avi.jpg)