Let’s say I have 4 scenes. The first scene contains only one empty gameobject that has another empty gameobject as a child. The parent gameobject has a script using the function DontDestroyOnLoad() so when the other scene loads it will not be destroyed. The child gameobject uses a script to spawn objects at different positions and it uses public Transform spawnPositions to get the positions.
The other 3 scenes have one parent gameobject which is empty and contains this script that uses public Transform positions; . Also the parent contains 3 empty gameobject that are basically just the positions that i need to access. So one of the scenes contains three positions, the other one three positions and the last one three positons too.
Now I just want to access the positions on each scene and pass it to the script that spawns the object.
So when the spawn script is loaded on one of the scenes it uses the positions of that scene and so on.
I have searched a lot and didn’t find a proper solution. I also have tried dozens of ways to achieve it and no success.
Hi;
as u said your main script wont destroy and its a good idea;
so the script that should get the new positions show be this script;
other scripts in different scenes just hold the transforms and they should not do any thing ;
first u have to do this on " OnLevelWasLoaded " function ;
this happens when u load a new scene ;
then u can search for the right object and get the component and positions ;
for example if the component that hold your positions in the scene name is " PositionHolder" and it has a array named “position” , and the array in your main script is named “AllPositionsInScene” , then u should do it like this ;
void OnLevelWasLoaded()
{
for (int i = 0; i < AllPositionsInScene.Length; i++)
{
AllPositionsInScene _= GameObject.FindObjectOfType<PositionHolder>().position*;*_
}
} this will override the previous positions and set them to new ones that are in the loaded scene;
The problem seems to be something else which i haven’t solved yet.
In the main script i have the codes that make the ‘gamecontrol’ object to load on other scenes without getting destroyed. In this gamecontrol i have just declared a public static GameControl control;
Now, I erased all the codes i was trying to fix and made an experiment to know where the problem was. I created a UI text in order to display some information from the spawn class that loads on each scene without being destroyed. I used these codes :
public Text text;
void Start()
{
text.text = GameControl.control.GetComponent<SpawnScript>().spawnPositions.Length.ToString();
}
The error I got is the same that was displayed before:
“NullReferenceException: Object reference not set to an instance of an object
System.Single.ToString () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Single.cs:241)”
Maybe there is some strange going on with the static variable ?