The problem might be related to that I’m calling these methods inside an PostProcessBuild-script. Inside this script I actually wanna find a GameObject with a certain component, but this is impossible, since for some reason the current scene is an invalid (or empty) one.
Therefore I wanna look for the Scene of my choice and then look for the GameObject, but as I said before, it doesn’t work.
The ONLY thing that seem to work is using:
EditorSceneManager.OpenScene(“Assets/MyScenes/TestScene.unity”)
and then this (see below) will return the correct amount GameObjects.
var objs = GameObject.FindObjectsOfType();
Is this intended somehow (because it’s inside PostProcess-script)? Or is it a bug?
So for GetSceneByXXX to work the scene has to be loaded.
class Test : MonoBehaviour
{
public IEnumerator Start()
{
SceneManager.LoadScene("TestScene");
yield return null;
Scene tScene = SceneManager.GetSceneByName("TestScene");
}
}
or
class TestMenu
{
[MenuItem("Test/OpenTestScene")]
public static MyOpenScene()
{
EditorSceneManager.OpenScene("Assets/TestScene.unity");
Scene tScene = SceneManager.GetSceneByName("TestScene");
}
}
But why is it that I need to open the scene when it’s already open? Or rather, it was open, but when building (inside the post process script) it closes the scene and GetSceneByXXX no longer works.
Also, what is the point of GetSceneAtIndex(int) if you have to actually load a scene before it to work? If I understand things correctly, GetSceneAtIndex(int) should return a scene by it’s index in “Build Settings”, is it not?
When building it closes your current scene, then one by one it opens each of the scene you have setup in buildsettings and serializes those to a format the standalone player can read.
So during build there is only scene open at a time and GetSceneByXXX can only get you that one scene.
GetSceneAtIndex is an API that is strange and I am considering to kill because of the misunderstandings. But no it is not the scenes in the build settings. In fact scene manager cannot get you any scenes in build settings only currently open/loaded scenes.
GetSceneAtIndex will get you the scene from the scene managers internal array of scenes if you open one scene in single mode and then load a scene additive there will be 2 scenes loaded and valid input to GetSceneAtIndex is 0 and 1.
Okay, thanks for the answer @SteenLund ! And indeed the GetSceneAt(…) is very confusing!
So, what would you suggest if I need to access a MonoBehavior on a GameObject (to get a value from it) during post-process? Is OpenScene the only way? I would optimally want to keep a reference to the Scene beforehand, so that I can just access the GameObject without loading the complete scene (because that is heavy, isn’t it?). If I had a scene reference there is some property that keeps the root game objects that I was thinking of using.
I am not sure I fully understand, but are you trying to get a value from a game object from a different scene than the one currently being build while building a player?
No, sorry if I’m unclear @SteenLund ! I only have one scene in my project. I’m building it to the windows platform, and I have one Post-Processing Script (PPS) that is ran (atferwards?). And when my PPS is invoked, I want to access a GameObject with a MonoBehaviour on it, kinda like this:
var something = Object.FindObjectOfType();
But when doing this, it will return null because the scene that is currently active is not MY scene it’s some temp scene (or invalid scene). Therefore for some reason, I have to call this:
@SteenLund I know this is old, but I have a question. Are there any known issues where UnloadSceneAsync fails with an exception that the scene was invalid when used with a scene path but succeeds with just the scene name? I have this issue on Android but I’m not yet able to reproduce it in another project.