- Home /
Changing spawning position with C#
I have a limited knowledge of coding so please bear with me. I have this code:
using UnityEngine;
using System.Collections;
public class EnemySpawning : MonoBehaviour {
public int SpawnCount;
public bool Position1;
public bool Position2;
public GameObject SpwanPosition;
public GameObject Enemy;
// Use this for initialization
void Start () {
SpawnCount = 2;
Position1 = false;
Position2 = false;
SpwanPosition = GameObject.Find("EnemySpawn");
Enemy = GameObject.Find("Enemy");
}
// Update is called once per frame
void Update () {
if(SpawnCount > 0)
Instantiate(Enemy, SpwanPosition, Quaternion.identity);
SpawnCount -= 1;
}
void ChangeSpawn () {
if(GameObject.Find("Enemy").transform.position == 0, 0, 0) {
Position1 = true;
}
if(Position1 = true)
SpwanPosition = GameObject.Find("EnemySpawn2");
}
}
I have 2 Gameobjects, "Enemy" and "EnemySpawn." Enemy is well, the enemy in the game. EnemySpawn is an empty Gameobject when the enemy spawns, there are currently 2 in the scene. I want it to spawn an enemy at the first EnemySpawn and then spawn another enemy at the second EnemySpawn. Any help is appreciated.
Answer by Memige · Oct 25, 2012 at 12:21 AM
Okay, looks like there is a decent amount of love this script could use. First off we need to address the way you are creating these enemies. Instantiating objects off of already instantiated objects is non-ideal, it can create issues if you destroy the original. Instead you will want to create a prefab for your enemy. Next, GameObject.Find is a hog and we should avoid it anytime we can. Instead, we can create a public GameObject array to store all of our spawn points in, and then iterate through it in script to use them.
using UnityEngine;
using System.Collections;
public class EnemySpawning : MonoBehaviour {
public int SpawnCount;
public bool Position1;
public bool Position2;
public GameObject[] SpawnPosition;
public GameObject Enemy;
// Use this for initialization
void Start () {
SpawnCount = 2;
Position1 = false;
Position2 = false;
}
// Update is called once per frame
void Update () {
if(SpawnCount > 0)
{
GameObject.Instantiate(Enemy, SpawnPosition[SpawnCount-1].transform.position, SpawnPosition[SpawnCount-1].transform.rotation);
SpawnCount--;
}
}
}
This will now require some addition setup in your editor. After you have created your Enemy prefab, just drag and drop it on the Enemy variable in the inspector for the object that has your EnemySpawning script. Then select both EnemySpawn objects in your scene Hierarchy and drag and drop then onto the SpawnPosition variable again in the inspector for the object that has your EnemySpawning script.
This has the added benefit of rotating your enemy to the same orientation as the EnemySpawn object, allowing you to easily update in the editor the direction a given Enemy will face when spawned.
Answer by speedything · Oct 25, 2012 at 12:25 AM
A few points. First, when comparing a value (as opposed to setting it) you need two "=" sign. -
if(Position1 = true) // This should be (Position1 == true)
You also should be careful of this
if(GameObject.Find("Enemy").transform.position == 0, 0, 0)
Floats are very rarely exactly on an integer value. I'd either allow a range (between say -0.5 and 0.5 on each axis), test distance from the point, or use a collider to trigger if something is in it. The last is my preferred as its more intuitive and easy to edit.
Finally,
SpwanPosition = GameObject.Find("EnemySpawn2");
You have a typo "Spwan", but you also will with Unity hunting down the game object every time you reassign it, Far better to set it in Start and call it later..
//In Start
GameObject spawn1 = GameObject.Find("EnemySpawn1");
GameObject spawn2 = GameObject.Find("EnemySpawn2");
//Spawn enemy
if(position1 == true)
{
//Instantiate at spawn2.transform.position
}
else if (position2 == true)
{
//Instantiate at spawn2.transform.position
}
Unity does account for small variations when comparing vectors. if (GameObject.Find("Enemy").transform.position == Vector3.zero) will work correctly.
Your answer
Follow this Question
Related Questions
How would I go about enemy spawning? (C#) 1 Answer
Trying to make a spawning enemy code 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make my enemies spawn at spawn point, but not randomly? 1 Answer