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:
-
Create all of my playable characters as a prefab, and tag them with a common Unique Identifier (“player”)
-
“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
-
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;