The question is answered, right answer was accepted
How to avoid cross-scenes reference errors when testing a single one in Unity ?
Hi everyone,
In my Unity learning project, i'm using references to the UI object present in the bootstrap menu (Game jam menu template in my case) in other scenes. For instance in the game or tutorial scene (i tweaked the template to support more scenes to learn a bit more...), when i want to reuse the music or scene management scripts :
public class ExitTutorial : MonoBehaviour {
private StartOptions startScript;
void Awake () {
startScript = GameObject.FindGameObjectWithTag("UI").GetComponent<StartOptions>();
}
....
}
Everything's ok when i test a build, or if i load the Bootstrap menu in Unity before launching the built-in game test, but if i launch the test directly from my tutorial scene, i get the NullReference error. I understand why, but i wonder how i can test this single scene when i'm working on it without the error
I found 2 solution but i'm not 100% satisfied with them
1) Encapsuate this code with #if UNITY_EDITOR, but in that case i can't test the references when i launch the boostrap scene & access another one from the game view
2) Deactivate "Error pause" in the console, but i'm sure it's not a good habit and i'll end up missing important stuff...
I'm sure i'm just missing the proper way to do it :) Thanks in advance
Answer by gkfr · Dec 08, 2017 at 11:20 PM
Silly me...
if(GameObject.Find ("UI"))
{
Debug.Log ("No need to instantiate UI, can use the references");
}
else Debug.Log ("Doesn't exist, instantiate UI prefab or don't use the UI related references");
Follow this Question
Related Questions
Unity Tests Deactivates Test Object 0 Answers
Deserialization during Unit Testing 0 Answers
Type or namespace of certain packages could not be found after unity testing framework was set up 0 Answers
Memory Profiler shows assets are loaded that are not referenced anywhere in scene 0 Answers
developerConsoleVisible? 1 Answer