- Home /
¿Score system with the new UI?
Hello there, I'm trying to do is: When the player colliding with the GOLD COIN, add 150 points to a text of the new UI and displayed on the screen. I saw that this could be done in a tutorial Unity video official, but as I did not understand, and I get an error. GOLD COIN to collide with the player, this object(gold coin)gets the variable SCORE of a script that has the text canvas and there adds 150 points. But this goes to collide with an error. "NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator ".
How do I fix this? Sorry, I use google translator, thank you very much.
Part of the script: GOLD COIN
var hudscore : GameObject;
function Awake(){
hudscore = GameObject.Find("ScoreNumero");
hudscore.GetComponent(Puntajes);
}
function OnTriggerEnter (collision : Collider) {
if(collision.gameObject.CompareTag("Player")&& posible == 1){
sumarpuntaje();
}
}
function sumarpuntaje(){
hudscore.score += 150;
diamante.renderer.enabled = false;
Destroy (gameObject);
}
Text CANVAS import UnityEngine; import UnityEngine.UI; var Texto : Text; public var score : int; function Awake(){ Texto.GetComponent(Text); } function Update(){ Texto.text = "Score : " + score; }
hudscore is a gameobject not a component on a gameobject, so it has no field "Score". Save the reference from "`hudscore.GetComponent(Puntajes);`": At Top:
var p : Puntajes;
In Awake:
p = hudscore.GetComponent(Puntajes);
In sumarpuntaje():
p.Score += 150;
Your answer
Follow this Question
Related Questions
How do I get a component of canvas? 0 Answers
Make canvas follow WASD but NOT camera,Make canvas follow WASD BUT NOT follow Camera 0 Answers
Script updates prefab but not instantiated clone of prefab 0 Answers
UI elements within canvas are invisible 0 Answers
UI elements within canvas are invisible 4 Answers