- Home /
Why is it that whenever i load a new scene while playing some of my linked texts dont work????
Okay, I made a game, in this game I have stores, the stores have texts within them I link these texts using codes such as :
public Text TextObjectName and I link my texts in the game engine, and it all works well and fine, until I load a new scene while playing the game, when that happens the text is not linked anymore, the text does not change and I get an error saying :
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.
but if I start my game from that scene it works fine, it is only when I load it while I am playing when I get this error and it doesn't work, which is confusing me a lot, and I don't know what to do.
if you need to know I use the code: SceneManager.LoadScene(SceneNumber); and yes the arrangement of the scenes in the build index is fine and the scene is opening all well except for the texts.
if you know anything about how to solve this please tell me.
EDIT: the texts that are not being linked are texts that are children of a button, I don't know if that helps, but the buttons are also linked in my code, and it also doesn't work, I think it might be the use of public gameobject in the case of the buttons and the public text in the use of the text, is there an alternative way that I can link my objects in a way that when I change scenes they stay linked???
Answer by ecv80 · Nov 19, 2018 at 09:01 AM
You probably want to call that in the Awake/Start part of some script using a reference to the root GameObject (such as Canvas) holding your Texts as children, in order to preserve them across different scenes.
void Start() {
DontDestroyOnLoad(GameObject.Find("Canvas"));
}
Original answer: You probably want to call that in the Awake/Start part of some of your script where you have a reference to your Texts, in order to preserve them across different scenes.
it didn't work but I got this message:
DontDestroyOnLoad only work for root GameObjects or components on root GameObjects. UnityEngine.Object:DontDestroyOnLoad(Object) Store:Start() (at Assets/Scripts/Store.cs:515)
do you know why?
I don't really know, but I suspect the GameObjects in question need to be at the root of your hierarchy. $$anonymous$$inda makes sense... Since you're most likely using Text somewhere within your Canvas tree, you might want to try to preserve the full canvas by calling DontDestroyOnLoad on it ins$$anonymous$$d of simply on the Text GOs. Tell me how it goes.
i guess it worked, now all the buttons are working and the game is functioning, but i use playerprefs to load some data, and not everything is loading correctly moving scene to scene, but ill figure this one on my own, thank you very much for your help