Hello everyone, I’m new to Unity and C# I require a detailed help.
I’m currently creating my first self project where 3 platforms that the player shouldn’t go and 1 platform that s/he should. These platforms are created around the player on specific positions (up, down, left, right). So I try to spawn these platforms on 4 random locations using a spawner location list. My aim is to create 3 false platforms on random locations without overlapping each other, and creating 1 true platform overlapping no other platform. My code to spawn platforms are in Spawner() function. But if I try to run my game, i get an error saying my randomSpawnerIndex is out of range on falsePlatform2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public PlayerController movement;
public Transform player;
public Transform playerPlatform;
public Transform cameraPosition;
public GameObject truePlatform;
public GameObject falsePlatform;
public GameObject Spawn1;
public GameObject Spawn2;
public GameObject Spawn3;
public GameObject Spawn4;
private Vector3 platformYOffset;
private Vector3 cameraYOffset;
bool gameHasEnded = false;
public float restartDelay = 1f;
void Start()
{
Spawner();
}
public void EndGame()
{
if(gameHasEnded == false)
{
gameHasEnded = true;
Invoke("Restart", restartDelay);
}
}
//RESTART
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void OnTrueMove()
{
platformYOffset = new Vector3(0, (float)0.5, 0);
GameObject[] falsePlatforms = GameObject.FindGameObjectsWithTag("FalsePlatform");
foreach (GameObject falseOne in falsePlatforms)
{
Destroy(falseOne);
}
truePlatform.GetComponent<Transform>().position = new Vector3(0, -3, 0);
platformYOffset = new Vector3(0, (float)0.5, 0);
cameraYOffset = new Vector3(0, 8, 0);
playerPlatform.position = player.position - platformYOffset;
cameraPosition.position = player.position + cameraYOffset;
Spawner();
}
void Spawner()
{
List<Vector3> spawnLocs = new List<Vector3>();
spawnLocs.Add(Spawn1.transform.position);
spawnLocs.Add(Spawn2.transform.position);
spawnLocs.Add(Spawn3.transform.position);
spawnLocs.Add(Spawn4.transform.position);
Debug.Log(spawnLocs.Count);
platformYOffset = new Vector3(0, (float)0.5, 0);
int randomSpawnerIndex = Random.Range(0, spawnLocs.Count);
GameObject instantiatedFalse1 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
spawnLocs.RemoveAt(randomSpawnerIndex);
Debug.Log(spawnLocs.Count);
GameObject instantiatedFalse2 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
spawnLocs.RemoveAt(randomSpawnerIndex);
Debug.Log(spawnLocs.Count);
GameObject instantiatedFalse3 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
spawnLocs.RemoveAt(randomSpawnerIndex);
Debug.Log(spawnLocs.Count);
GameObject instantiatedTrue = Instantiate(truePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
spawnLocs.RemoveAt(randomSpawnerIndex);
}
}
I tried to create the true platform using the last element of the spawnLocs list.
I look forward to your help and any suggestions that make my very basic game mechanic become real.