- Home /
Understanding Prefabs + Player Spawn Points
So I'm still trying to understand the workflow of Unity - I've learned a lot in the last couple days of just diddling around, but I'm still confused about how I can spawn a different character in my scene (e.g., Character Select)
Here's what I do know so far:
1) Create all of my playable characters as a prefab, and tag them with a common Unique Identifier ("player")
2) "Character select" simply consists of creating a scene/menu that saves the selected character to PlayerPreferences (selectedCharacter="characterMia"), and when a level is loaded, the scene loads the selected character in the scene
3) Any script during gameplay that needs to access my player's data (e.g., GUI) that isn't a part of of character prefab needs to find the GameObject by tag ("player") and cache the variable for quick access later
Here's what I have trouble wrapping my head around:
I guess it pertains to item #2 above. Currently within the editor, I can throw down any player prefab I have currently into the scene and it works. I can adjust the position and rotation, hit play, and that's where the gameplay will start at -- except for the fact that that scene will ALWAYS feature that one prefab that I chose at design time.
So, how does instantiation work in Unity? By design, do I have to include any one character prefab on the map at startup (to hold position and rotation) and then swap it out with the character the player selected on startup? Or do I just have to throw down a generic "SpawnPoint" gameobject throughout the map and create a player prefab out of thin air to that spawnpoint's location and rotation (map starts with no player prefab and gets loaded at runtime).
I found this code from the UnityDocs:
// Instantiates respawnPrefab at the location
// of the game object with tag "Respawn"
var respawnPrefab : GameObject;
var respawn = GameObject.FindWithTag ("Respawn");
Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation);
(I assume this should only be used for the initial loading of the scene, otherwise each time the player spawns you'd create another copy of the character in the map.)
All of this shouldn't be hard to do - if I tag all of my character prefabs with "player" to expedite finding them in the map - but I'm not sure how it works. I understand the theory behind it, but not the how-to.'
EDIT: I was browsing the answers section and found this question, Instantiate a prefab through code in C# They gave this example (C#)
var instance : GameObject = Instantiate(Resources.Load("enemy"));
So, couldn't I throw all of my character prefabs into a folder named "Resources", name them accordingly, ("characterMia", "characterFrank", etc.) and then upon scene startup:
// Instantiates respawnPrefab at the location
// of the game object with tag "Respawn"
var characterName : String;
characterName = (insert code here to get character from PlayerPrefs)
var respawnPrefab : GameObject = Instantiate(Resources.Load(characterName));
var respawn = GameObject.FindWithTag ("Respawn");
respawnPrefab.transform = respawn.transform;
respawnPrefab.rotation = respawn.rotation;
Answer by DoctorWhy · Jul 23, 2012 at 08:43 PM
Instantiating an object during the game is fine as far as I know. I beleive Unity handles making sure you don't reload the model every time, but instead just adds a new character to a list of objects. That code you found should work above.
I don't know if you want to use it with respawning a character though, but instead resetting the player information (such as health), then moving them to the spawn point. (Unless you are restarting pieces of the level as well). However, spawning a new enemy at a spawn point with that should be perfectly fine.
EDIT: Just saw your edit. (That code is Javascript, not C#).
It originally was C# but I quickly changed it to JS when I posted it in my edit
Answer by Seth-Bergman · Jul 23, 2012 at 08:45 PM
well, you don't need to be using PlayerPrefs, that's more for saving game data between sessions. If you create a static var, that won't be lost when you change scenes.
var player : Transform;
static var currentCharacter : Transform;
var respawn = GameObject.FindWithTag ("Respawn");
player = Instantiate (currentCharacter, respawn.transform.position, respawn.transform.rotation);
yes, each time you run this, it would instantiate another copy.. so if you are going to instantiate a different character, you would want to destroy the previous.. by using "player =" above, I am setting my var "player" to the object instantiated, so I can access its scripts now by
var myScript = player.gameObject.GetComponent(MyScript);
myScript.DoSomething();
myScript.someFloatVar = 0.5;
EDIT:
forgot, as for the position/rotation, no you don't need a "dummy" character to start.. above, we are adopting the spawnPoint's transform, with
...respawn.transform.position, respawn.transform.rotation);
Ok, well there's only going to be one player per scene. If the player dies, the whole entire scene gets reloaded (by design). So I would in theory put this code in the "Start()" of a "GameController" object of sorts, that keeps track of level time in the scene, player score, etc.? The GameController would hold a variable that contains the player, and when it's created, create the player and position appropriately within the level?
Your answer
Follow this Question
Related Questions
Player or character select 2 Answers
I need to teleport a FPS chara controller 2 Answers
How Move Player between Scenes? 7 Answers
How to keep same player settings from the scene in the .exe file 0 Answers