Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » JS Conversion Help.

JS Conversion Help.

View previous topic View next topic Go down  Message [Page 1 of 1]

1 JS Conversion Help. on Tue Oct 27, 2015 2:12 pm

BeerHuntor


Member [Lv. 1]
Member [Lv. 1]
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

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum