I'm trying to create a character selection menu in Unity, where each character has their own version of the level that follows.
There are about 10 characters. Each character has a different location where they spawn in the level, and each enemy has a different spawn position as well, depending on which character was selected.
What I have been doing is to create separate levels (scene files) for each character. Then in the character selection menu, I load the level depending on the level selected by the user.
This seems to be a lot of code and data repetition, as the scene is the same in all levels; it's just that the spawn positions and enemies are different.
Is there any way have only one level scene, and get the spawn position and enemy spawns based on the character?
This is the code I have so far in GameManager.cs
:
public GameObject enemy;
public Transform enemySpawnPoint;
public GameObject player;
public Transform playerSpawnPoint;
public static GameManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
void Start()
{
StartCoroutine("SpawnEnemies");
SpawnPlayer();
}
IEnumerator SpawnEnemies()
{
while (true)
{
yield return new WaitForSeconds(1.0f);
SpawnEnemy();
}
}
public void SpawnEnemy()
{
Vector3 enemySpawnPos = enemySpawnPoint.position;
Instantiate(enemy, enemySpawnPos, Quaternion.identity);
}
public void SpawnPlayer()
{
Vector3 playerSpawnPos = playerSpawnPoint.position;
Instantiate(player, playerSpawnPos, Quaternion.identity);
}
...and the character selection script:
public void Level1()
{
SceneManager.LoadScene(1);
}
public void Level2()
{
SceneManager.LoadScene(2);
}
// ...
public void Level10()
{
SceneManager.LoadScene(10);
}
Now the issue is, if I want to make any changes in one scene, like the skybox or background color, I will have to open all the other level scenes and make the changes.
Another thing is that if I want to add new levels, I will have to do the same thing, which seems like a lot of repetition.
Is there some way to set the enemy prefab, enemy spawn positions, player prefab and player spawn position once the character has been selected?
For example, if the user clicks on level1, all the preceding variables are set automatically and so on.....