0
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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