Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've encountered a strange problem. In my game I have a prefab called UiManager. It has two public Text-fields, one for score, and one for lives. I only have one instance of this prefab and I've assigned the two Text-fields with two text-objects. Yesterday it worked well.

enter image description here

Today I reopened my project and started the game and when I collected a life I got a null reference on the text field Lives. Now clearly it is not null, I tried to delete my instance and recreate it and then assigning the Text-fields again, and it worked.

I then saved my project and exited Unity and reopened my project and now the issue was back again, a null reference on Lives. Also I noticed that it is not possible to apply ui-objects to a prefab (?).

Anyway, does anyone have any idea why this occurs?

Have a nice day!

share|improve this question

There must be only one reason in my view.

If you are trying to attach any object to prefab from hierarchy then your information will be last till exactly that very prefab exists.

Means if you create a new instance of that prefab then your fields will be empty.

But YES your prefab will save the reference if you drop elements from Unity Project Explorer, not from Hierarchy.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.