I'm trying to create a simple scene transition. On entering a collider, there is a black canvas that's slowly faded in over the duration of 3 seconds. Then, a new scene is loaded, and the scene slowly fades from black to invisible. Now, I've almost finished it. However, I'm running into a small problem I cannot seem to fix.
The animation consists out of two text elements and one image. The hierarchy looks like this:
SceneLoader
is an empty object, SceneFader
is a canvas. Image
is a black image stretched across the screen, and RegionName
and RegionSubtext
are two text elements that are filled as soon as the transition begins.
The code I'm using to load the scene is this:
private IEnumerator _loadSceneWithTransition(string sceneToLoad, Vector3 spawnCoordinates, string regionName, string regionSubtext) {
this.screenFade.SetTrigger(SceneLoader.AnimatorTrigger);
GameObject.Find("SceneLoader/SceneFader/Image/RegionName").GetComponent<Text>().text = regionName;
GameObject.Find("SceneLoader/SceneFader/Image/RegionSubtext").GetComponent<Text>().text = regionSubtext;
yield return new WaitForSeconds(4f);
SceneManager.LoadScene(sceneToLoad);
GameObject.Find("SceneLoader/SceneFader/Image/RegionName").GetComponent<Text>().text = regionName;
GameObject.Find("SceneLoader/SceneFader/Image/RegionSubtext").GetComponent<Text>().text = regionSubtext;
GameObject.Find("Player").GetComponent<PlayerMovementController>().spawnCharacterToLocation(spawnCoordinates);
}
Now, this doesn't work as I'd expect it to. The first part runs perfectly fine: The animation is started, the text is displayed and the scene changes. However, that's the part where everything breaks. As soon as the scene loads, there is no text. Also, the player position is not set to where it should be.
I really do not know why, the only reason I could think of was that the script was called in the first scene, and therefore, the GameObject.Find()
calls return the elements in that scene, not in the currently used one (actually, I'm fairly sure of that because GetInstanceID()
returns the same for both calls). Now, the problem at this part is that I do not know how to make that work properly. One idea I had was to do try and fetch a new instance of the script with something like: GameObject.Find("SceneLoader").GetComponent<BaseSceneLoader>()
and work with that, but then I remembered I still need GameObject.Find()
for this, which would probably yield the result of the old scene, so it's not helping at all. On top of that, I'm not even sure if that is the actual problem.
I'm kinda out of ideas here. I also do not know what's the best practice in that situation, and I couldn't find anything meaningful on DDG either.
Lastly, I've created a small video clip showing off the problem. I hope this helps in identifying the issue.
regionName
andregionSubtext
and use the tags instead of the name? I can't really come up with another idea of how to find a specific game object in my scene, and always thought that, if I have like 250 bazillion tags, the entire game will become extremely messy. \$\endgroup\$