So i found another question relating to this on the boards but found no answer so I’m asking in regards to my specific error. The game actually still works perfectly fine but I’m getting an error at the bottom of the screen that reads NullReferenceException: Object reference not set to an instance of an object. Both errors point to my PlayerController script. The first says “PlayerController.SetCountText () (at Assets/Scripts/PlayerController.cs:44)”. The second reads “PlayerController.OnTriggerEnter (UnityEngine>Collider other) (at Assets/scripts/PlayerController.cs:38)” I’m assuming those lines are telling me where in my code I’m having problems but wtf I can’t figure it out. As far as I can tell my code is exactly the same as in the Roll A Ball tutorials. Here’s my Player Controller code:
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4
5 public class PlayerController : MonoBehaviour {
6
7 public float speed;
8 public Text countText;
9 public Text winText;
10
11 private Rigidbody rb;
12 private int count;
13
14 void Start ()
15 {
16 rb = GetComponent<Rigidbody> ();
17 count = 0;
18 SetCountText ();
19 winText.text = "";
20 }
21
22 void FixedUpdate ()
23 {
24 float moveHorizontal = Input.GetAxis ("Horizontal");
25 float moveVertical = Input.GetAxis ("Vertical");
26
27 Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
28
29 rb.AddForce (movement * speed);
30 }
31
32 void OnTriggerEnter(Collider other)
33 {
34 if (other.gameObject.CompareTag("Pick Up"))
35 {
36 other.gameObject.SetActive(false);
37 count = count + 1;
38 SetCountText();
39 }
40 }
41
42 void SetCountText ()
43 {
44 countText.text = "Count: " + count.ToString ();
45 if (count >= 9)
46 {
47 winText.text = "You Win!";
48 }
49 }
50 }
51
Thanks in advance for any help!