Wave spawner script
How can I spawn more than one object in my script "objects to spawn"
or at least a different way to spawn more than one object in this script:
// C#
// WaveGenerator.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class WaveAction
{
public string name;
public float delay;
public Transform prefab;
public int spawnCount;
public string message;
}
[System.Serializable]
public class Wave
{
public string name;
public List<WaveAction> actions;
}
public class WaveGenerator : MonoBehaviour
{
public float difficultyFactor = 0.9f;
public List<Wave> waves;
private Wave m_CurrentWave;
public Wave CurrentWave { get {return m_CurrentWave;} }
private float m_DelayFactor = 1.0f;
public GameObject ObjectToSpawn;
IEnumerator SpawnLoop()
{
m_DelayFactor = 1.0f;
while(true)
{
foreach(Wave W in waves)
{
m_CurrentWave = W;
foreach(WaveAction A in W.actions)
{
if(A.delay > 0)
yield return new WaitForSeconds(A.delay * m_DelayFactor);
if (A.message != "")
{
// TODO: print ingame message
}
if (A.prefab != null && A.spawnCount > 0)
{
for(int i = 0; i < A.spawnCount; i++)
{
// Random position within this transform
Vector3 rndPosWithin;
rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
Instantiate(ObjectToSpawn, rndPosWithin, transform.rotation);
}
}
}
yield return null; // prevents crash if all delays are 0
}
m_DelayFactor *= difficultyFactor;
yield return null; // prevents crash if all delays are 0
}
}
void Start()
{
StartCoroutine(SpawnLoop());
}
}
I guess you added "ObjectToSpawn" to the script? Why did you do that? Before the script, was indeed capable of spawing different enemys!
No it wasn't!!! I had to add the "Object to spawn" there to make it spawn objects. What I want is to spawn different enemies at the same time!!
Answer by Oribow · Oct 06, 2015 at 07:33 PM
What if you change the type of "prefab" in waveaction class to "GameObject"? Then you could set the prefabs to what ever you want in the inspector. Also change your spawning, to instantiate the prefabs. Now you can
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class WaveAction
{
public string name;
public float delay;
public GameObject prefab;
public int spawnCount;
public string message;
}
[System.Serializable]
public class Wave
{
public string name;
public List<WaveAction> actions;
}
public class WaveGenerator : MonoBehaviour
{
public float difficultyFactor = 0.9f;
public List<Wave> waves;
private Wave m_CurrentWave;
public Wave CurrentWave { get { return m_CurrentWave; } }
private float m_DelayFactor = 1.0f;
IEnumerator SpawnLoop()
{
m_DelayFactor = 1.0f;
while (true)
{
foreach (Wave W in waves)
{
m_CurrentWave = W;
foreach (WaveAction A in W.actions)
{
if (A.delay > 0)
yield return new WaitForSeconds(A.delay * m_DelayFactor);
if (A.message != "")
{
// TODO: print ingame message
}
if (A.prefab != null && A.spawnCount > 0)
{
for (int i = 0; i < A.spawnCount; i++)
{
// Random position within this transform
Vector3 rndPosWithin;
rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
Instantiate(A.prefab, rndPosWithin, transform.rotation);
}
}
}
yield return null; // prevents crash if all delays are 0
}
m_DelayFactor *= difficultyFactor;
yield return null; // prevents crash if all delays are 0
}
}
void Start()
{
StartCoroutine(SpawnLoop());
}
}
And why it didnt help? If you dont tell, I cant help you. As far as I can see it, the script should be fully capable of spawning diffrent Enemys per round.
There was an error saying something about monobehaviour is something, then when I check in the script monobehaviour was red. Sorry for my poor description but I can't remember most of it. Thank you for replying back and for your help. @ oribow
Answer by moisespirela · Aug 01, 2016 at 02:50 PM
Just make an array of objects to spawn, and a for loop to go through each object in the array. Simple.
Your answer
