Okay so I’m making my own changes to the Roll-a-Ball tutorial from the Unity site: https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial
I’m trying to add multiple levels in different scenes and I’m having trouble bringing the text over between the scenes. I keep getting this error when I touch a collectible in the second scene.
MissingReferenceException: The object of type ‘Text’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
My code is here:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour {
public float speed;
public int score = 0;
public int thisLevel;
public Text countText;
public Text scoreText;
public Text winText;
public Text startText;
public AudioClip mwam;
public AudioClip sparkle;
public int[] countMax;
public static PlayerController instance = null;
private Rigidbody rb;
private int count;
private int finalLevel = 2;
private bool win;
// Update () is called before rendering a frame.
// FixedUpdate () is called just before computing a physics calculation.
// CTRL + ' searches API for highlighted text.
void Start () {
rb = GetComponent<Rigidbody> ();
count = 0;
win = false;
winText.text = "";
if (thisLevel == 0) {
startText.text = "Welcome!
" +
"Touch the yellow cubes for points!
" +
"Collect them all to win!
" +
“Black cubes take away points; stay away!”;
} else {
startText.text = “”;
}
SetCountScoreText ();
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
rb.AddForce (movement * speed);
if (win && Input.GetButton("Jump") && (thisLevel != finalLevel)) {
Application.LoadLevel (thisLevel + 1);
transform.position = new Vector3 (0, 1, 0);
count = 0;
}
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.CompareTag ("PickUp")) {
other.gameObject.SetActive (false);
count = count + 1;
score = score + 50 * count;
startText.text = "";
}
else if (other.gameObject.CompareTag ("BadItem")) {
score = score / 2;
AudioSource.PlayClipAtPoint (mwam, Camera.main.transform.position);
}
else if (other.gameObject.CompareTag ("GrandPickUp")) {
other.gameObject.SetActive (false);
count = countMax[thisLevel];
score = score + 50 * countMax[thisLevel];
startText.text = "";
winText.text = "You found the secret tree!
";
AudioSource.PlayClipAtPoint (sparkle, Camera.main.transform.position, 0.25F);
}
SetCountScoreText ();
}
void SetCountScoreText () {
countText.text = "Count: " + count.ToString ()
+ "/" + countMax[thisLevel];
scoreText.text = "Score: " + score.ToString ();
if (count >= countMax[thisLevel] && !win) {
SetWinText ();
}
}
void SetWinText () {
win = true;
winText.text += "Congratulations!
" +
“You won with a score of " + score.ToString ();
if (thisLevel != finalLevel) {
winText.text += "
You may press the Space key”
+ "
to jump to the next level.“;
} else {
winText.text += "
You may now close the game.”;
}
}
void Awake() {
if (instance == null)
instance = this;
else
Destroy (transform.gameObject);
DontDestroyOnLoad (transform.gameObject);
}
}
I don’t know what text I’m destroying but if someone could help, that’d be great. I really just can’t pinpoint where the problem is coming from.