I am following along with the Roller Ball tutorial found on unity website but that is written in C# and to give me an added learning curve, im converting it to JavaScript:


I am currently on the above video,  I have everything working apart from the UI and updating the score on the UI.


Code:

#pragma strict

import UnityEngine.UI;
 
 var rb : Rigidbody;
 var speed : float ;
 
 var countText : Text;
 
 private var score : int;

function Start() {
 rb = GetComponent.<Rigidbody> () ;
 score = 0;
 SetCountText ();
}
 
function FixedUpdate(){
 var moveHorizontal : float = Input.GetAxis ("Horizontal");
 var moveVertical : float = Input.GetAxis ("Vertical");
 
 var movementSpeed : Vector3 = new Vector3 (moveHorizontal , 0 , moveVertical) ;
 rb.AddForce(movementSpeed * speed );
}

function OnTriggerEnter (other : Collider) {
 if (other.gameObject.CompareTag ("Pickup")) {
 other.gameObject.SetActive(false);
 score = score+1;
 SetCountText ();  

 };
}
function SetCountText () {
 countText.text = "Count: " + score.ToString () ;
}

 




This is my script and s you can see from the video at 7.45 I have everything I need but when I go back to unity I get a compiler error of "NullReferenceException: Object reference not set to an instance of an object playerController.SetCountText() (at Assets....playerController.js:35)"

I can't for the life of me figure out what I have missed.  Any help would be greatly appreciated.  The game works with the error, just doesn't update the UI how it should.

Thanks.


EDIT : - I Have figured out the solution, leaving  this up for anyone else that may be having issues.  The solution was I was accessing the "text" variable of the "Text Script" without defining which Object it was suppose to be using.  

Adding the

Code:


countText = GameObject.Find("Canvas/Count Text").GetComponent(Text);

fixed the issue