- Home /
Points not adding to score in unity text
Hi,
I am creating a game where when you tap/click on a falling object it is removed and then adds a point to your score.
Currently, I have written the code and added the text but when I click on the object is does remove but no point is added to the score.
This is my object remove script;
public class OnClickDestroy : MonoBehaviour {
void OnMouseDown()
{
Destroy (gameObject);
}
}
and here is my adding points script;
public class PointsScript : MonoBehaviour {
public Text Text;
public int starValue;
private int score;
void start () {
score = 0;
UpdateScore();
}
void OnMouseDown(){
score += starValue;
UpdateScore();
}
void UpdateScore(){
Text.text = "Score: \n" + score;
}
}
I have attached the script to a tap controller gameobject with the necessary values.
alt text
This is my text settings ![alt text][1]
Thank you [1]: /storage/temp/102933-onmousedown-withscript.png
Answer by Epic0007224 · Sep 30, 2017 at 11:03 PM
Check if it’s actually running by putting a Debug.Log in the OnMouseDown void. If it’s not running then it’s a problem with the OnMouseDown void.
Hi, Thank you for replying. I tried to run the debug but nothing came through on the log.
i feel like im missing something but i cant work it out.
I think this is because your object (the one that gets destroyed) intercepts your click. There will only be one click action per time.
Answer by gwnguy · Oct 01, 2017 at 12:33 AM
Line 6 of PointsScript - does "start" need to be capitalized?
Hi, Thank you for replying. I changed the start to capital but still nothing happening.
Answer by YoucefB · Oct 03, 2017 at 12:20 PM
There are Two possible solutions for this, you can either move the Destroy part to the points script To become like this:
void OnMouseDown(){
score += starValue;
UpdateScore();
Destroy (this.gameObject);
}
Or change the execution order of the OnClickDestroy script to exexute after PointsScript: https://docs.unity3d.com/Manual/class-ScriptExecution.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Only Allow One Click on GameObject? 2 Answers
Looping and closing walls 1 Answer