# Unity associated game object is null when running

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

• Include your script, maybe you wrote something wrong.. – zoran404 May 17 '15 at 13:20
• This is difficult to diagnose from the description you've given. As zoran404 says, seeing your script would help, as well as a screenshot of your scene hierarchy highlighting the objects in question. A common mistake with inspector variables is referencing a prefab instead of a scene object or vice versa, so something like that might be at work here. – DMGregory May 17 '15 at 16:15
• By any chance, is another script causing the GameObject referenced by Brokenboxref to be destroyed at runtime? – DMGregory May 17 '15 at 17:58
• Upgrading might cause some referencing issues as far as I can tell from my own little experience. It might be a bug introduced by upgrading the project. – Varaquilex May 18 '15 at 21:11
• Note 3 kind of illustrates the solution, It's a bit like the magical rebuild option for Visual studio. I've encountered several problems with *.meta files that were solved by having Unity rebuild its library as well – Athos vk Dec 18 '15 at 9:15

This is very strange. I've heard of MonoBehaviour null checks not be consistent but an object just becoming null? Strange.

A few first bases to cover:

• You're not destroying Brokenboxref

• Brokenboxref is serialized in the inspector, or at least exists when you use it

• You're not assigning Brokenboxref as anything else

I'm not sure what could be wrong if all these things are right. If creating a new project works, that's what you should do.

As requested, I am adding my own note 3 as an answer as it solved the problem.

The problem is solved when the Library folder is deleted and Unity is forced to rebuild it. This did not seem to cause any other problems

• Don't forget to mark this answer "accepted" so the system knows the problem is solved. Otherwise the question keeps floating to the top of the list to try to attract new answers. ;) – DMGregory Dec 6 '16 at 2:12