- Home /
Move my entire player gameobject in the next scene (using loadlevel)
Hey all,
I'm working on a platformer and I have one scene which is supposed to be like open world and a small building with a door that's supposed to be a shop building. I'd like when player gets to the door and press UP to be able to spawn in the new scene called house.unity. I tried several ways and read around dozen of websites/answers to the similar issue but couldn't find a complete answer :/
There are two main issues: a) I want to keep the player, for example even tho player starts first as a prefab, once the user is playing the game, player is supposed to gather weapons and those show up on the character as well.. Simply instantiating a basic player prefab in a store is a big no-no.. + I want to keep player's score, money, attributes etc..
b) I want to keep track of x/y coordinates of door entrance, so when I later want to exit the shop I'll be spawned just outside it..
Now I read A LOT about PlayerPrefs, DontDestroyOnLoad, OnLevelWasLoaded or even using a static variables but still find it hard to find the best or working way :/
So the scenario is: -Player is spawned into a world at a start of a new game by simply instantiating player prefab at designated spot.
-Camera isn't father/child of player, it's another GameObject floating in hierarchy that follows player via code.
I have a world and a door. Door has a trigger and is supposed to enable loading of level "house_01.unity".
The "house_01.unity" contains scene with merchant and a door. Does this scene needs another camera? a spawn point?
How can I TAKE my player, totally cut and paste it at "house_01.unity" at lets say empty gameobject with name "spawnMeHere" and also move a camera into that scene as well?
It might looks like I'm lazy and I want YOU to do the work instead of me, but I spent a good 3-4hrs just looking for solution and I'm not getting it :(
Thanks all
Answer by Olgo · Feb 25, 2014 at 10:21 PM
You can transfer data between scenes by writing data to a static variable. You could have one class that stores all your transferable information.
public static class GameSettings{
public static Character save;
}
You can write to this by having a line whenever you change scenes like "GameSettings.save = characterStats"
or something like that. use as many variables as you need or save the whole class. However, this wont save when you shut down your game. You need to use player preferences for something like that.
Also, to spawn your character at a certain location, have a script that moves the character when the scene loads. You can have an empty gameobject on the location you want him to appear. The script could say something like "player.transform.location = GameObject.FindObjectWithTag("spawnLocation").transform.location;" Where spawnLocation is the tag on your empty gameobject denoting your spawn point.
Additional info, each of your scenes does need its own camera and spawn point unless you have it attached to the player prefab as part of the prefab
So, what you're saying is that ins$$anonymous$$d of me looking for a way to in a sense "cut and paste" my player every time I go to the new scene, I should keep a list of information, and each time I load a new scene spawn a new, generic prefab of my player and on the run-time assign different info that corresponds to player's progress in the game?
The trouble I'm having with moving my player around a newly loaded screen is because after line:
...Application.LoadLevel(X);
Any code after that isn't executed, so I'm a bit confused as to where that code, in your case "player.transform.position =...." should be?
Thanks for helping!
not a problem.
What I would do is have an empty GameObject that appears in every scene that has a script that does the following in the "Start()" function.
Instantiate playerPrefab, find spawnPoint gameObject for current scene, set playerPrefab's location to spawnPoint's location, set player's stats to the last saved stat (for example, gold = saved gold; health = savedHealth; score = savedScore;), whatever else you need
2h later and I solved it! :) Thank you for pointing me in the right direction!
Your answer
Follow this Question
Related Questions
scene loader issue 0 Answers
Reloading Level problem 0 Answers
getting loading progress for a level 1 Answer
Animated Loading Screen Unity Indie 0 Answers