- Home /
Scene Connecting
Alright,
So I ran into a bit of a problem with the linking.
I know that when creating a new level (new star system in my case) you place the player down where they will arrive/start.
Well going to the new star system works beautifully. My question is, when I return back through the gate, I start where the player first starts on the otherside of the system.
How can I correct this to get the player to actually arrive at each gate?
Answer by Bampf · Nov 27, 2009 at 03:49 PM
You need at least one object to maintain game state across levels.
Often people have a GameObject called "GameController" or "GameManager" or something like that, that performs game logic and keeps track of information that needs to be passed from level to level. To keep the object from going away, call DontDestroyOnLoad on it.
The tricky part is making sure that you only have one of these objects around. You don't want the first scene to create a new one when the player returns to it. There are various ways around this. See the Unity Wiki's article on Singletons for some good examples. (That code also solves another problem for you, which is how your scripts can easily and efficiently finding the manager object to use.)
Confused on the singletons, cause the coding looks like its being done in C#, and I'm doing this in Javascript.
The wiki also has a separate article: A$$anonymous$$anagerClass, which includes a Javascript singleton. It's older and there's less explanation for it but maybe it will help.
http://www.unifycommunity.com/wiki/index.php?title=A$$anonymous$$anagerClass
Answer by runevision · Nov 27, 2009 at 03:47 PM
In each level you can place some spawn points on the locations where the player should be able to appear. You could have multiple spawn points with different names. You can make a spawn point script that you can attach to an empty game object to make it a spawn point.
You need to have a persistent object (one that is not destroyed when you load a new scene). When entering a gate, you tell this object at which named spawn point in the new scene the player should be spawned at. The persistent object could either be some controller object, or it could just be the player himself.
You can create persistent objects either by using Object.DontDestroyOnLoad. Alternatively, if you only need some persistent data and not a whole gameobject, you can store the data in static variables of a class.
When the new level has loaded, the persistent object spawns the player at the right spawn point, of if the player himself is persistent, you can just move him to the right point. You can use the OnLevelWasLoaded function on the persistent object for that.
I don't know how to make a spawn point script, can you point me in the right direction?
You probably don't even need a spawn point script. You can just give your spawn points a special tag, and then use FindGameObjectsWithTag to get a list of all spawn points in the current level. Then select the right one of those based on the GameObject names, and move your player to the position of that spawn point GameObject.
Answer by TowerOfBricks · Nov 27, 2009 at 03:48 PM
The best thing is probably to not create a new player for each scene you load, so tag it with DontDestroyOnLoad if you haven't done so already.
Next, in the player script you would want to put a variable stating if it should start at the start or at a gate, if if it should start at the gate, then define which gate (if you have more than one gate). So when a new scene loads, it will look for if it should place the player at the gate or at the start and place it where it should be.
This is a valid approach, it just keeps the player object around for all levels ins$$anonymous$$d of a separate manager object. In fact you might want to do both, have a manager object and keep the player object from scene to scene.
You still have to figure out when the player object is created and how to keep it from being created more than once. A singleton class is one way. Another is to have each player object destroy itself if it isn't the first one on the scene. I've gotten that way to work but it's a bit fragile and I don't really recommend it.
Answer by HeywoodFloyd · Jan 08, 2010 at 04:15 PM
I needed to communicate between levels, so I wrote a C# script with nothing but static members and methods. I had a private Dictionary to hold named data, accessor methods to add, remove, or change the stored data.
Because it didn't have anything but static methods, I didn't inherit from Monobehaviour. I had to put it in the standard assets/scripts folder for it to be accessible to other scripts.
This has the advantage of not requiring any special game objects or set up in the Unity editor.
Pretty much. It was a bit more general, as it stored object references, but we used it like the PlayerPrefs class, which I didn't know about.
Answer by WILEz1975 · Dec 20, 2015 at 05:33 PM
But... if in my scene the player destroy an enemy (gameobject)... when exit ed re-enter (load scene) in this scene... i dont want the destroyed gameobjects...
Your answer
![](https://koobas.hobune.stream/wayback/20220613171901im_/https://answers.unity.com/themes/thub/images/avi.jpg)