So I have a script in which case Ive defined three spawn points by empty game objects in my scene. I have a for loop that basically: Checks the first empty, sees if it wants to spawn something there, if it does, it instatiates an enemy and sets its positon to the spawn object. the way I have the for loop set up, I thought each sprite could only get assigned to each position once, then it would quick, but I keep getting instances of 2 sprites layed over top of each other in the exact same transform position, please help!
code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject enemy;
public List<GameObject> enemyList;
public GameObject[] spawnPoints;
private float _x;
private float _y;
private float enemyCount = 0;
List<ScriptableObject> spawnableEnemies = new List<ScriptableObject>();
bool spawnerActive = false;
bool spaceFilled = false;
int i = 0;
private void Start()
{
for (int i = 0; i <= spawnPoints.Length; i++)
{
if (enemyCount == 0)
{
Object.Instantiate(enemy);
enemy.transform.position = spawnPoints[0].transform.position;
enemyList.Add(enemy);
enemyCount++;
}
else if (enemyCount > 0)
{
checkSpawnState();
//checkSpace();
if (spawnerActive == true)
{
Object.Instantiate(enemy);
enemy.transform.position = spawnPoints[i].transform.position;
enemyList.Add(enemy);
enemyCount++;
}
Debug.Log(i);
}
}
}
bool checkSpawnState()
{
float picker = Random.Range(-1, 1);
if (picker < 0)
{
spawnerActive = false;
}
else if (picker >= 0)
{
spawnerActive = true;
}
return spawnerActive;
}
}
You may be running into an error which is why you’re getting a double spawn. Your for loop is incorrect.
It should be
i < spawnPoints.Length
Otherwise, let’s say you have 3 spawn points. Because collections are 0 based, in your implementation, you start with 0, 1, 2, and then 3. Once it is at 3 and hits line 45, it will throw an out of index exception because there is no index of 3. In your case you’re trying to tell it that you have 4 spawn points, not 3. Getting an error would potentially create issues. Try fixing that first.
Also, line 29 and line 45, all you are doing is moving your prefab around, not the thing you instantiated, which is returned from Instantiate() call.
You need to assign that returned reference to a temporary variable and move that new instance, not the prefab.
Similarly, line 31 and line 46 all you are doing is adding the reference to your prefab to the enemy list (the same reference again and again), which is likely not helpful either.
Also, why have enemyCount at all? Just use the .Count property on the list of enemies.
I have enemy Count to make sure the number of enemies on the list corresponded to the number of on the field and the inpector, so it was more for debugging. I should again mention I am an art student and not very experienced yet, but I do love to learn! So if you would go into how I properly assign the value to the thing I’m instantiating? I think that’s what you’re saying? I would deeply appreciate it!
Now everything that your code above USED to do to enemy, you must instead do those things to ThisParticularEnemy: positioning it, adding it to lists, anything else that you mean to do to that specific enemy.
This is the code that ended up working [: Thank you guys!
posting the final for anyone that may be building a similar system or having a similar problem in the future.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject enemy;
public List<GameObject> enemyList;
public GameObject[] spawnPoints;
private float _x;
private float _y;
private float enemyCount = 0;
List<ScriptableObject> spawnableEnemies = new List<ScriptableObject>();
bool spawnerActive = false;
int i = 0;
private void Start()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
if (enemyList.Count == 0)
{
//Object.Instantiate(enemy);
GameObject newEnemy = Instantiate(enemy);
newEnemy.transform.position = spawnPoints[0].transform.position;
enemyList.Add(newEnemy);
enemyCount++;
}
else if (enemyList.Count > 0)
{
checkSpawnState();
if (spawnerActive == true)
{
//Object.Instantiate(enemy);
GameObject newEnemy = Instantiate(enemy);
newEnemy.transform.position = spawnPoints[i].transform.position;
enemyList.Add(newEnemy);
enemyCount++;
}
Debug.Log(i);
}
}
}
bool checkSpawnState()
{
float picker = Random.Range(-1, 1);
if (picker < 0)
{
spawnerActive = false;
}
else if (picker >= 0)
{
spawnerActive = true;
}
return spawnerActive;
}
}