Hello, I want to have only 2 objects in the scene. When i call the method it spawns 2 objects no problem with that but when I destroy one of them and call the method again and it spawns another 2 so in my scene are 3 how to limit or check if there is already 2 objects so its spawns only one. Guys dont hate me with my code.
Spawn Manager
public class SpawnMana : MonoBehaviour
{
public GameObject stone;
public GameObject water;
public GameObject copper;
public GameObject iron;
public GameObject organism;
public GameObject animals;
public GameObject dinosaurs;
public GameObject humans;
public GameObject village;
public GameObject castle;
public int numObjects = 0;
public List<GameObject> objectsAdd;
private MainMenu menu;
void Start()
{
menu = GameObject.Find("MainMenu").GetComponent<MainMenu>();
if (menu.a == true)
{
numObjects++;
objectsAdd.Add(stone);
}
SpawnSpheres();
}
Vector3 RandomCircle(Vector3 center, float radius, int a)
{
Debug.Log(a);
float ang = Random.value * 360;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.z = center.z;
return pos;
}
public void SpawnSpheres()
{
int index = Random.Range(0, objectsAdd.Count);
Vector3 center = transform.position;
for (int i = 0; i < numObjects; i++)
{
int a = 360 / numObjects * i;
Vector3 pos = RandomCircle(center, 7.0f, a);
Quaternion rot = Quaternion.LookRotation(Vector3.forward, center - pos);
Instantiate(objectsAdd[index], pos, rot);
}
}
public void Water()
{
numObjects++;
objectsAdd.Add(water);
}
}
And DetectCollisions
public class DetectCollisions : MonoBehaviour
{
private float xRange = 45;
private float yRange = 20;
private SpawnMana spawnMana;
private GameManager gameMana;
// Start is called before the first frame update
void Start()
{
spawnMana = GameObject.Find("SpawnMana").GetComponent<SpawnMana>();
gameMana = GameObject.Find("GameManager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
if (transform.position.x > xRange)
{
Destroy(gameObject);
}
if (transform.position.x < -xRange)
{
Destroy(gameObject);
}
if (transform.position.y > yRange)
{
Destroy(gameObject);
}
if (transform.position.y < -yRange)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
if (!CompareTag("BB"))
{
Destroy(gameObject);
}
if (CompareTag("Stone"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(1 , 0, 0, 0, 0,0,0,0,0,0);
}
if (CompareTag("Water"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0,1,0,0,0,0,0,0,0,0);
}
if (CompareTag("Copper"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0,0,1,0,0,0,0,0,0,0);
}
if (CompareTag("Iron"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0,0,0,1,0,0,0,0,0,0);
}
if (CompareTag("Organism"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 1,0,0,0,0,0);
}
if (CompareTag("Animals"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 0,1, 0,0,0,0);
}
if (CompareTag("Dinosaurs"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 0, 0, 1,0,0,0);
}
if (CompareTag("Humans"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 0, 0, 0,1,0,0);
}
if (CompareTag("Village"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 0, 0, 0,0,1,0);
}
if (CompareTag("Castle"))
{
spawnMana.SpawnSpheres();
gameMana.UpdateScore(0, 0, 0, 0, 0, 0, 0,0,0,1);
}
}
}