- Home /
Different spawn points from different scenes ? [Solved]
Hi guys,
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.
Do you have any idea how i can accomplish this ?
Thank you. :)
Answer by Cuttlas-U · Apr 09, 2017 at 04:40 PM
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[i] = GameObject.FindObjectOfType<PositionHolder>().position[i];
}
}
this will override the previous positions and set them to new ones that are in the loaded scene;
Hi, thank you for your response. The thing is that the "PositionHolder" is not located in the scene where the main script is not destroyed but at the other scenes that hold the positions where the object are going to be spawned. When I wrote this function as you said it gave me an error "The type or namespace 'PositonHolder' could not be found. Are you missing an assembly reference?"
I am explaining it again in case it helps you understand better whats going on.
The spawn script contains this code:
public class Instantiate : $$anonymous$$onoBehaviour {
//Here spawnPositions used to get assigned with drag and drop on inspector using three gameobjects but on the same scene as this script and it worked fine...
public Transform[] spawnPositions;
//I am using an array here so to assign different objects to spawn
public Rigidbody[] objectPrefab;
Rigidbody object1;
Rigidbody object2;
//Replaced this the function exactly as you said....
void OnLevelWasLoaded()
{
for (int i = 0; i < spawnPositions.Length; i++)
{
spawnPositions [i] = GameObject.FindObjectOfType<PositionHolder> ().position [i];
}
}
void Update () {
//random index inside spawnPositions is generated in another function below which i didn't include on this code...it works and its not needed...
object1 = Instantiate (objectPrefab [0], spawnPositions [random].position, Quaternion.Euler(randomRot, randomRot, randomRot)) as Rigidbody;
object2 = Instantiate (objectPrefab [1], spawnPositions [random].position, Quaternion.Euler(randomRot, randomRot, randomRot)) as Rigidbody;
}
}
The script above is attached to the child gameobject of the main script that doesn't destroy on load ( which is attached to the parent gameobject).
Now at the other scenes i have a hierarchy of gameobjects like this: 1- PositionHolder(a script is attached in this gameobject and inside script is only this code : "public Transform[] positions;" 1.1 - Position1 (child gameobject of 1), 1.2- Position2 (child gameobject of 1), 1.3 - Position3 (child gameobject of 1),
In case you didn't undertand something pls tell me. I have been trying a lot of other codes today and none of them has worked. Have no idea why. Thank you.
Answer by G3R1 · Apr 09, 2017 at 09:47 PM
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 ?
Guys I finally found the problem even though i don't understand it why did it happen. The problem was with the spawn script attached to the child gameobject of the main script gameobject. The spawn script should be attached to the main gameobject and not the child so the static variable applies the reference to Transform[] array, otherwise it will not pass those values from on array to the other. It will keep saying null reference. $$anonymous$$aybe there is a good explanation about this but I am not capable to find out why for the moment. You can try the scenario above in case you want to find the real exaplanation by yourslef. Thank you for the time.