I am just starting with Unity. I have two UI Text game objects, Atext and Btext. I have associated them by drag-dropping with my PlayerController script public variables, say public Text atext and public Text btext. Before running, the Unity editor correctly shows that Atext and Btext are correctly associated with the public variables. When running with debug, I get a null reference exception with btext and even the Unity Editor shows the association as None (Text). After stopping, the association turns to Btext as it is supposed to be. The script is very simple and I don't set btext to null at any point.
When I create a new UI Text object by duplicating Atext, it works correctly after associating it with some new variable. When I create a new UI Text object by Create-> UI -> Text, it doesnt work
Might be a stupid question but what am I forgetting?
Edit: Here is the problem again with code:
Before running with debug, the editor shows
PlayerController2 (Script)
Script: PlayerController2
Speed: 7
Countboxref: Countbox(Text)
Winboxref: Winbox (Text)
Brokenboxref: Text (Text)
where Countbox, Winbox, and Text are Canvas/UI/Text objects which I have drag dropped into place. Winbox object has been created by duplicating it from Countbox, Text has been created by Create -> UI -> Text.
enter using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController2 : MonoBehaviour {
public float speed;
public Text Countboxref;
public Text Winboxref;
public Text Brokenboxref;
private Rigidbody rb;
private int boxit;
void Start ()
{
Countboxref.text = "count";
Winboxref.text = "winner";
Brokenboxref.text = "broken";
}
When the program breaks on line Brokenboxref.text = "broken”, the editor shows
Brokenboxref: None (Text)
and when the execution is stopped completely, it again returns to
Brokenboxref: Text (Text)
Note 1: In this project, every Text object created with Create->UI->Text is broken. A new Text object works only when I select Countbox or Winbox and duplicate it. Thus it is not possible that the object would be explicitly destroyed in the code. I can create as many Text objects as I wish by copying (all don't work) and as many Text objects as I wish by duplicating (all will work).
Note 2: when I create a new project from scratch, of course everything works as expected. I assume the project is somehow corrupted.
Edit 2: No prefabs or such. Just the basic roll a ball tutorial. But this was my son's project so I am not sure what he has done when following the tutorial.
Note 3: The problem went away when I deleted the Library folder and forced Unity to rebuild it. Don't know yet if I caused any other problems by doing that. I saw that somewhere as a suggestion for corrupted projects. I don't know if there is a proper way to refresh.
Also, this project was originally started with Unity 4.6 and upgraded to 5.02