JS Conversion Help

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.

#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.

Make sure you’ve assigned an UI element with a Text component to the countText in the inspector.

I already have done that. if that is what you are meaning, sorry I’m new to unity

It looks like you’re not actually assigning the “Count Text” gameobject in your script. So Unity has no idea what “Text” UI object you’re referring too, and thus the “null reference”. Usually, in my own experience, null reference errors are Unity whining that I have not specifically told it what object to use.

In your case, I’d do this. In your Start() function, try adding this line, and make sure you add this line before the line that calls your SetCountText() function (so you could even add it as the very first line in your Start function):

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

Right now it looks like your SetCountText() function is simply accessing the “text” variable in the “Text” script on your “Count Text” game object in the hierarchy. It can get a bit confusing when everything is named or contains the word “Text” lol. But your “countText” variable is the key here, and it’s defined as a “Text” type – which is actually referring to a “Text” script component.

So would using countText = GetComponent.<Text> (); not work as I have tried that also.

I have also tried countText = GetComponentOfChild<Canvas> (); and nothing… sorry for the questions just trying to wrap my head around it so when i come across this again I know what im doing instead of just copying a bit of code and not knowing what it does thus being clueless as to using it in the appropriate way again. Thanks for the reply, ill try it out

EDIT: - By the way your fix works like a charm. Thanks.

Well your first example would work. But only if you place your script (i.e. add it as a component) to your “Count Text” game object underneath your Canvas. But then if you did that, other code in your script might not work, depending on how you reference certain things in your code.

Your second example won’t really work, but mainly due to syntax and what that function actually does.

So, I’m just curious though, did the line of code I give you actually make it work?

Well believe me, I know exactly what you mean. And really, it just comes down to experience, and knowing how Unity does things. Which can be hard and frustrating a lot. But it’s worth it I think :slight_smile:

So keep asking questions. And hopefully people will be kind enough to help you try to learn it. I know I didn’t give you a good conceptual answer to those questions, but they’re the right questions to ask.

Yeah it did work thanks. I should of maybe watched the video one more time, because apparently there was a little bit i missed in the first watch, that would of solved this issue. I didnt actually assign the object to the player object in the inspector, which would of been alot easier than coding it in. But now i know both ways for future reference, and know what to look for if i get the same problem again. I had to re read it a few times, but I got what you were trying to relay without issue on your answer, so thank you. There is alot more of friendly people who are willing to help in this community than I have previously encountered on others so that is a bonus. Thanks for the quick reply.

Can I ask why? What value do you get from learning UnityScript?