I'm creating a spaceshooter clone and I like to achieve the following.
In the menu the player can choose between 3+ ships(characters). When the player hits the "Start Game" Button the game starts in a different scene and is flying the selected ship.
Therefore I have done the following:
I have 2 scenes. "MainMenu" and "Game". In "MainMenu" I have a few buttons and a GameControl Object. The GameControl object will not be destroyed on load. I followed this tutorial to have this.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public static int WIDTH = 800;
public static int HEIGHT = 600;
public static bool FULLSCREEN = true;
public static int REFRESHRATE = 60;
public GameObject playerShip;
public float health;
public float experience;
public float score;
void Awake()
{
if (instance == null)
{
Screen.SetResolution(WIDTH, HEIGHT, FULLSCREEN, REFRESHRATE);
DontDestroyOnLoad(gameObject);
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 30), "Health: " + health);
GUI.Label(new Rect(10, 40, 100, 30), "XP: " + experience);
GUI.Label(new Rect(10, 70, 100, 30), "Score: " + score);
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void loadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
public void quitGame()
{
Application.Quit();
}
public void setShip(GameObject ship)
{
Destroy(playerShip);
playerShip = Instantiate<GameObject>(ship);
playerShip.SetActive(false);
}
}
I also followed this tutorial about character selection. But in the video it is done a little bit different than I need it. All my ships are prefabs like it is done in the tutorial. So in "MainMenu" I have a ShipSelection that looks like that:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ShipSelection : MonoBehaviour {
private int selectionIndex = 0;
public List<GameObject> availableShips;
// Use this for initialization
void Start () {
availableShips = new List<GameObject>();
foreach(Transform t in transform)
{
availableShips.Add(t.gameObject);
}
foreach(GameObject ship in availableShips)
{
ship.SetActive(false);
}
nextShip();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
}
public void nextShip()
{
availableShips[selectionIndex].SetActive(false);
selectionIndex++;
if(selectionIndex >= availableShips.Count)
{
selectionIndex = 0;
}
availableShips[selectionIndex].SetActive(true);
GameControl.instance.setShip(availableShips[selectionIndex]);
}
public void previousShip()
{
availableShips[selectionIndex].SetActive(false);
selectionIndex--;
if(selectionIndex < 0)
{
selectionIndex = availableShips.Count - 1;
}
availableShips[selectionIndex].SetActive(true);
GameControl.instance.setShip(availableShips[selectionIndex]);
}
}
I thought that when I call GameControl.instance.setShip(availableShips[selectionIndex]);
I could pass an instance of the selected ship to the next scene.
But when I change the scene the value is null
. I think this is because the objects from the "MainMenu" are all destroyed and so is the reference to the selected ship...
But how can I achieve to pass a "selected Ship" to the next or another scene, so that this scene knows which ship should be spawned/instantiated?
If you need to look into the project you can find it at
github.com/Naxos84/SpaceShooterGame
If something is unclear please do not hesitate to request more information.
Apparently I don't have enough reputation to post more than 2 links. So sorry for the plain repo link.
EDIT: I found another tutorial on character selection.
youtube.com/watch?v=ERuxfU8ArqA
But this video is using all "player object" in the game scene too. Is this really the only way of doing a character selection? Or is there an even better way?