Spawn Script (request)
Spawn Script (request)
To start I have no coding/scripting experiance. I have no idea where to begin with what I'm after.
I want to make a simple little shooter where my player character sits behind a barrackade shooting at waves of bad guys (zombies and robots ect). I have the basics to my player character set up as well as the scene all I need now is a script to spawn waves of enemies which instantly attack my player character.
What i'm after is:
I have 4 areas within my scene I'd like to spawn a random amount of bad guys which increases once each wave has been cleared. for instance, on game load spawn point1 spawns a random amount of bad guy(between1-5, 5 seconds later spawn point2 loads a random amount1-5, spawn point3 spawns 5-10 as well as spawn point4. when each wave has been cleared I'd like the script to instantly spawn another wave with increased enemy amounts ie wave 2 sp1 and 2=10-15 bad guys sp3 and 4=20-30 bad guys then with wave 3 sp1 and 2=20-30 ans sp3 and 4 30-40
The idea behind the game is for the player to be over run with bad guys quickly with their score recorded on a high scoreboard.
If anyone is able to help with this it would be greatly appretiated.
Mouff
I want to make a simple little shooter where my player character sits behind a barrackade shooting at waves of bad guys (zombies and robots ect). I have the basics to my player character set up as well as the scene all I need now is a script to spawn waves of enemies which instantly attack my player character.
What i'm after is:
I have 4 areas within my scene I'd like to spawn a random amount of bad guys which increases once each wave has been cleared. for instance, on game load spawn point1 spawns a random amount of bad guy(between1-5, 5 seconds later spawn point2 loads a random amount1-5, spawn point3 spawns 5-10 as well as spawn point4. when each wave has been cleared I'd like the script to instantly spawn another wave with increased enemy amounts ie wave 2 sp1 and 2=10-15 bad guys sp3 and 4=20-30 bad guys then with wave 3 sp1 and 2=20-30 ans sp3 and 4 30-40
The idea behind the game is for the player to be over run with bad guys quickly with their score recorded on a high scoreboard.
If anyone is able to help with this it would be greatly appretiated.
Mouff
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: Spawn Script (request)
I hope this helps:
Create an empty game object and add the attached script (Spawner) to it. Now you just choose the options from the inspector.
You have Easy Enemy, Medium Enemy, Hard Enemy and Boss Enemy. Place your enemy prefab in to one of those slots. Total Enemy determines how many of that prefab to spawn. Leave Spawn checked, Spawn Type Time Wave. Wave timer (How long in seconds before the next wave is spawned from that location.)
If you want more then one spawn, simply create another empty game object and drop the script.
Note: When you add the script to the empty game object, you will see a red square. You will not see this during game play!! Since this is a C# script, make sure you save your script name as: Spawner
Create an empty game object and add the attached script (Spawner) to it. Now you just choose the options from the inspector.
You have Easy Enemy, Medium Enemy, Hard Enemy and Boss Enemy. Place your enemy prefab in to one of those slots. Total Enemy determines how many of that prefab to spawn. Leave Spawn checked, Spawn Type Time Wave. Wave timer (How long in seconds before the next wave is spawned from that location.)
If you want more then one spawn, simply create another empty game object and drop the script.
Note: When you add the script to the empty game object, you will see a red square. You will not see this during game play!! Since this is a C# script, make sure you save your script name as: Spawner
Code: Select all
// FILENAME:Spawner.cs
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
//----------------------------------
// All the Enums
//----------------------------------
// Spawn types
public enum SpawnTypes
{
Normal,
Once,
Wave,
TimedWave
}
// The different Enemy levels
public enum EnemyLevels
{
Easy,
Medium,
Hard,
Boss
}
//---------------------------------
// End of the Enums
//---------------------------------
// Enemy level to be spawnedEnemy
public EnemyLevels enemyLevel = EnemyLevels.Easy;
//----------------------------------
// Enemy Prefabs
//----------------------------------
public GameObject EasyEnemy;
public GameObject MediumEnemy;
public GameObject HardEnemy;
public GameObject BossEnemy;
//----------------------------------
// End of Enemy Prefabs
//----------------------------------
//----------------------------------
// Enemies and how many have been created and how many are to be created
//----------------------------------
public int totalEnemy = 10;
private int numEnemy = 0;
private int spawnedEnemy = 0;
//----------------------------------
// End of Enemy Settings
//----------------------------------
// The ID of the spawner
private int SpawnID;
//----------------------------------
// Different Spawn states and ways of doing them
//----------------------------------
private bool waveSpawn = false;
public bool Spawn = true;
public SpawnTypes spawnType = SpawnTypes.Normal;
// timed wave controls
public float waveTimer = 30.0f;
private float timeTillWave = 0.0f;
//Wave controls
public int totalWaves = 5;
private int numWaves = 0;
//----------------------------------
// End of Different Spawn states and ways of doing them
//----------------------------------
void Start()
{
// sets a random number for the id of the spawner
SpawnID = Random.Range(1, 500);
}
// Draws a cube to show where the spawn point is... Useful if you don't have a object that show the spawn point
void OnDrawGizmos()
{
// Sets the color to red
Gizmos.color = Color.red;
//draws a small cube at the location of the gam object that the script is attached to
Gizmos.DrawCube(transform.position, new Vector3 (0.5f,0.5f,0.5f));
}
void Update ()
{
if(Spawn)
{
// Spawns enemies everytime one dies
if (spawnType == SpawnTypes.Normal)
{
// checks to see if the number of spawned enemies is less than the max num of enemies
if(numEnemy < totalEnemy)
{
// spawns an enemy
spawnEnemy();
}
}
// Spawns enemies only once
else if (spawnType == SpawnTypes.Once)
{
// checks to see if the overall spawned num of enemies is more or equal to the total to be spawned
if(spawnedEnemy >= totalEnemy)
{
//sets the spawner to false
Spawn = false;
}
else
{
// spawns an enemy
spawnEnemy();
}
}
//spawns enemies in waves, so once all are dead, spawns more
else if (spawnType == SpawnTypes.Wave)
{
if(numWaves < totalWaves + 1)
{
if (waveSpawn)
{
//spawns an enemy
spawnEnemy();
}
if (numEnemy == 0)
{
// enables the wave spawner
waveSpawn = true;
//increase the number of waves
numWaves++;
}
if(numEnemy == totalEnemy)
{
// disables the wave spawner
waveSpawn = false;
}
}
}
// Spawns enemies in waves but based on time.
else if(spawnType == SpawnTypes.TimedWave)
{
// checks if the number of waves is bigger than the total waves
if(numWaves <= totalWaves)
{
// Increases the timer to allow the timed waves to work
timeTillWave += Time.deltaTime;
if (waveSpawn)
{
//spawns an enemy
spawnEnemy();
}
// checks if the time is equal to the time required for a new wave
if (timeTillWave >= waveTimer)
{
// enables the wave spawner
waveSpawn = true;
// sets the time back to zero
timeTillWave = 0.0f;
// increases the number of waves
numWaves++;
// A hack to get it to spawn the same number of enemies regardless of how many have been killed
numEnemy = 0;
}
if(numEnemy >= totalEnemy)
{
// diables the wave spawner
waveSpawn = false;
}
}
else
{
Spawn = false;
}
}
}
}
// spawns an enemy based on the enemy level that you selected
private void spawnEnemy()
{
// To check which enemy prefab to instantiate
if (enemyLevel == EnemyLevels.Easy)
{
// Checks to see if there is a gameobject in the easy enemy var
if (EasyEnemy != null)
{
// spawns the enemy
GameObject Enemy = (GameObject) Instantiate(EasyEnemy, gameObject.transform.position, Quaternion.identity);
// calls a function on the enemy that applies the spawner's ID to the enemy
Enemy.SendMessage("setName", SpawnID);
}
else
{
//Shows a debug message if there is no prefab
Debug.Log("ERROR: No easy enemy Prefab loaded");
}
}
else if (enemyLevel == EnemyLevels.Medium)
{
// Checks to see if there is a gameobject in the medium enemy var
if (MediumEnemy != null)
{
// spawns the enemy
GameObject Enemy = (GameObject) Instantiate(MediumEnemy, gameObject.transform.position, Quaternion.identity);
// calls a function on the enemy that applies the spawner's ID to the enemy
Enemy.SendMessage("setName", SpawnID);
}
else
{
//Shows a debug message if there is no prefab
Debug.Log("ERROR: No medium enemy Prefab loaded");
}
}
else if (enemyLevel == EnemyLevels.Hard)
{
// Checks to see if there is a gameobject in the hard enemy var
if (HardEnemy != null)
{
// spawns the enemy
GameObject Enemy = (GameObject) Instantiate(HardEnemy, gameObject.transform.position, Quaternion.identity);
// calls a function on the enemy that applies the spawner's ID to the enemy
Enemy.SendMessage("setName", SpawnID);
}
else
{
//Shows a debug message if there is no prefab
Debug.Log("ERROR: No hard enemy Prefab loaded");
}
}
else if (enemyLevel == EnemyLevels.Boss)
{
// Checks to see if there is a gameobject in the boss enemy var
if (BossEnemy != null)
{
// spawns the enemy
GameObject Enemy = (GameObject) Instantiate(BossEnemy, gameObject.transform.position, Quaternion.identity);
// calls a function on the enemy that applies the spawner's ID to the enemy
Enemy.SendMessage("setName", SpawnID);
}
else
{
//Shows a debug message if there is no prefab
Debug.Log("ERROR: No boss enemy Prefab loaded");
}
}
// Increase the total number of enemies spawned and the number of spawned enemies
numEnemy++;
spawnedEnemy++;
}
// Call this function from the enemy when it "dies" to remove an enemy count
public void killEnemy(int sID)
{
// if the enemy's spawnId is equal to this spawnersID then remove an enemy count
if (SpawnID == sID)
{
numEnemy--;
}
}
//enable the spawner based on spawnerID
public void enableSpawner(int sID)
{
if (SpawnID == sID)
{
Spawn = true;
}
}
//disable the spawner based on spawnerID
public void disableSpawner(int sID)
{
if(SpawnID == sID)
{
Spawn = false;
}
}
// returns the Time Till the Next Wave, for a interface, ect.
public float TimeTillWave
{
get
{
return timeTillWave;
}
}
// Enable the spawner, useful for trigger events because you don't know the spawner's ID.
public void enableTrigger()
{
Spawn = true;
}
}
Re: Spawn Script (request)
Spirit, I love you I'm on te verge of offering to have your babies
Thanks m8, this is greatly appretiated.
Mouff
Thanks m8, this is greatly appretiated.
Mouff
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: Spawn Script (request)
WOW...um...it aint Thursday! HA! LOL...glad it helped or helps!Mouff wrote:Spirit, I love you I'm on te verge of offering to have your babies
Thanks m8, this is greatly appretiated.
Mouff
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Spawn Script (request)
you got to buy him a few drinks first
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: Spawn Script (request)
lol, just about to set it up, i'll let you know how it goes.
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: Spawn Script (request)
*crosses fingers*Mouff wrote:lol, just about to set it up, i'll let you know how it goes.
Re: Spawn Script (request)
Spirit,
I followed your instructions, but I'm getting this error.
SendMessage setName has no receiver!
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object)
Spawner:spawnEnemy() (at Assets\WeaponScripts\Spawner.cs:193)
Spawner:Update() (at Assets\WeaponScripts\Spawner.cs:127)
[..\..\Runtime\Mono\MonoExportUtility.cpp line 516]
Any idea what I've done wrong or the problem could be?
I followed your instructions, but I'm getting this error.
SendMessage setName has no receiver!
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object)
Spawner:spawnEnemy() (at Assets\WeaponScripts\Spawner.cs:193)
Spawner:Update() (at Assets\WeaponScripts\Spawner.cs:127)
[..\..\Runtime\Mono\MonoExportUtility.cpp line 516]
Any idea what I've done wrong or the problem could be?
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: Spawn Script (request)
I will have to look at the script as soon as I get home and look it over in my "test" game that uses it. I will let you know soonMouff wrote:Spirit,
I followed your instructions, but I'm getting this error.
SendMessage setName has no receiver!
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object)
Spawner:spawnEnemy() (at Assets\WeaponScripts\Spawner.cs:193)
Spawner:Update() (at Assets\WeaponScripts\Spawner.cs:127)
[..\..\Runtime\Mono\MonoExportUtility.cpp line 516]
Any idea what I've done wrong or the problem could be?