- Home /
Can I load new information into GameObjects when a new scene is loaded?
Hello everyone! So as the title says, I was wondering if there is some kind of way to load new info in already defined GameObjects in a script when a new scene is loaded. I'm a bit of a noob at programming, so sorry if I ask things that are impossible : p
So here is what I have now:
private GameObject Cam1;
private GameObject Cam2;
private GameObject Cam3;
void Awake()
{
Cam1 = GameObject.Find("CameraPosition01");
Cam2 = GameObject.Find("CameraPosition02");
Cam3 = GameObject.Find("CameraPosition03");
}
This a script on the Player. So when the Player gets transfered to a new scene, it should actually completely reload this script to find the new "CameraPosition..."s in the current scene. I tried a couple of things already, but nothing seems to work.
This player script is using DontDestroyOnLoad and you want to find the camera positions in the current scene?
Answer by KGD_Dude · Dec 11, 2018 at 09:07 AM
So, I was able to fix it.
I tried before to place it into "void OnSceneLoaded"
But it seems I had to place this in the beginning:
void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
So that did the trick. Thank you guys for the effort : )
I made a mistake, "void Start" worked for a small moment then stopped working. Fixed it with "void Awake" : )
Answer by Vega4Life · Dec 11, 2018 at 03:39 AM
If your player is staying around, via DontDestroyOnLoad, then I would listen for a scene change and get the camera positions then.
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Cam1 = GameObject.Find("CameraPosition01");
Cam2 = GameObject.Find("CameraPosition02");
Cam3 = GameObject.Find("CameraPosition03");
}
So, I tried this "void OnSceneLoaded" but did't seem to work either... What about destroying the script on SceneLoad and adding it again as a new? And there is a script on the player to DontDestroyOnLoad, but it's not in this script. This is not the problem right?
and the "Reset" function that darkStar27 mentioned will not work in a build? only in the editor?
Answer by darkStar27 · Dec 11, 2018 at 03:40 AM
I haven't tried anything like that yet but i think you can use Reset Reset is used to set the target to a default value.
For more information on rest and how to use it check out this link.
Your answer
Follow this Question
Related Questions
Can someone explain calling other scripts in C#? 2 Answers
Changing quality setting on all scenes 1 Answer
How to add force if spherecast is true and how to set direction of sphere 0 Answers
How can I slow the velocity of my player without it affecting fall speed? 0 Answers
How do I make a photography function? 0 Answers