Multi-Scene Editing & GameObject.Find

Example:
So I have my game-logic in scene A, and my UI in scene B. At the start of the game I use SceneManager.LoadScene(…, LoadSceneMode.Additive); to load both scenes into the game.
Now how do I find a game-object that’s in scene B from a script that’s in scene A? The simple GameObject.Find doesn’t seem to work in this case.

Thanks and cheers.

It sounds like GameObject.Find may be broken, but rather than fix that, let’s ditch GO.F entirely. You’ll be better off - GameObject.Find is terrible in many ways.

Seems like you need your in-game object and menu to communicate with each other. Use a singleton for your menu and/or player in that case:

public class SomeClass : MonoBehaviour {
public static SomeClass instance;
void Awake() {
instance = this;
}
public int whatever;
}

// anywhere else
SomeClass.instance.whatever = 5;
1 Like

Hi,

StarManta is right, try to avoid GameObject.Find. That said, Find() should work across all scenes, can you share some example code of it not working?

Be aware though that you cannot Find() the object immediately after calling LoadScene, you have to wait one frame for the scene to finish loading.

3 Likes

Thank you, even though I did wait one frame, I didn’t realise that the scene in which I was trying to find has not yet finished loading when I was trying to call Find().

1 Like