- Home /
Question by
Dimensionic · Apr 07, 2015 at 12:40 AM ·
c#guinullreferenceexception
[Roll-a-ball] Changing UI text (NRE)
Hi, trying to follow roll-a-ball tutorial. Stuck at displaying score. This is the error I get:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.SetScore () (at Assets/Scripts/PlayerController.cs:38)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:33)
This is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Rigidbody rbody;
public float speed ;
private int score = 0;
public Text scoreText;
void Start ()
{
rbody = GetComponent<Rigidbody> ();
scoreText = GetComponent<Text> ();
SetScore ();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rbody.AddForce (movement * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "pickup") {
other.gameObject.SetActive(false);
score++;
SetScore();
}
}
void SetScore()
{
scoreText.text = "Score: " + score;
}
}
Comment