- Home /
Spawn Point Repeating
Hello Unity community, i make this car spawn script. I have 2 problem about this script. 1- When i run game its normal working and spawn 6 car ( 3 Left side 3 right side) but when i trigger car spawner its spawn 12 car not 6 car. I cant fix it. 2- Sometimes cars spawn same spawn point how i can make it non repeating ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class SpawnSystem : MonoBehaviour
{
public GameObject[] cars;
public Transform[] leftSpawnPoint;
public Transform[] rightSpawnPoint;
void Start()
{
for (int i = 0; i < 3; i++)
{
Instantiate(cars[Random.Range(0, cars.Length)], leftSpawnPoint[Random.Range(0, leftSpawnPoint.Length)].position, Quaternion.Euler(0, 180, 0));
Instantiate(cars[Random.Range(0, cars.Length)], rightSpawnPoint[Random.Range(0, rightSpawnPoint.Length)].position, Quaternion.identity);
}
}
public void SpawnCar()
{
for (int i = 0; i < 3; i++)
{
Instantiate(cars[Random.Range(0, cars.Length)], leftSpawnPoint[Random.Range(0, leftSpawnPoint.Length)].position, Quaternion.Euler(0, 180, 0));
Instantiate(cars[Random.Range(0, cars.Length)], rightSpawnPoint[Random.Range(0, rightSpawnPoint.Length)].position, Quaternion.identity);
}
}
}
Delete everything in Start and make it like this:
void Start(){
SpawnCar();
}
Also don't call spawnCar() If start has spawned it
So try and comment Start()
Answer by mmertbbulut · Nov 30, 2021 at 09:40 PM
Okey i change it. But its still spawn 6 car first and 12 car more when enter trigger. 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class SpawnSystem : MonoBehaviour
{
public GameObject[] cars;
public Transform[] leftSpawnPoint;
public Transform[] rightSpawnPoint;
void Start()
{
SpawnCar();
}
public void SpawnCar()
{
for (int i = 0; i < 3; i++)
{
Instantiate(cars[Random.Range(0, cars.Length)], leftSpawnPoint[Random.Range(0, leftSpawnPoint.Length)].position, Quaternion.Euler(0, 180, 0));
Instantiate(cars[Random.Range(0, cars.Length)], rightSpawnPoint[Random.Range(0, rightSpawnPoint.Length)].position, Quaternion.identity);
}
}
}
Well as i said you are calling the function some where else to.
And don't post coments as answers.
Please convert it to comment.
Answer by luvm1n3craft · Dec 01, 2021 at 02:37 AM
Your spawn points are repeating because you're choosing a random spawn point every time you instantiate a new car. Try this:
public void SpawnCar()
{
for (int i = 0; i < 3; i++)
{
Instantiate(cars[Random.Range(0, cars.Length)], leftSpawnPoint[Random.Range(0, leftSpawnPoint.Length)].position, Quaternion.Euler(0, 180, 0));
Instantiate(cars[Random.Range(0, cars.Length)], rightSpawnPoint[Random.Range(0, rightSpawnPoint.Length)].position, Quaternion.identity);
}
}
Answer by mmertbbulut · Dec 01, 2021 at 01:43 AM
Okey i fix my problem when i remove my car collider its fixed but now i need to make my spawns no repeate. How i can make it ?
Your answer