So I was reading the Unity tutorial section and was trying to implement a spawn script based on the one they showed from “Spawning Enemies”.
I have 5 spawn points and am trying to spawn a randomly selected nuke (there are 3 total) at a randomly selected spawn point at increasing intervals of time. I didn’t get this far and am having trouble implementing the first part of the script.
public class SpawnNukeScript : MonoBehaviour {
public Transform[] spawnPoints;
public GameObject enemy;
public int spawnTime = 5;
// Use this for initialization
void Start () {
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
// Update is called once per frame
void Update () {
}
void Spawn () {
// Find a random index between zero and one less than the number of spawn points.
int spawnPoints = Random.Range (0, spawnPoints.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (enemy, spawnPoints[spawnPoints].position, spawnPoints[spawnPoints].rotation);
}
}
to keep things a bit simpler, try to change the name of your “int spawnPoints” in Spawn().
maybe just something like "int sp = " etc…
otherwise, you could modify: "Instantiate(enemy, this.spawnPoints[spawnPoints].position, this.spawnPoints[spawnPoints].rotation);
Which is just much longer to type lol
When you say random nuke, is that like a random enemy? There is only 1 of those listed, so if you want a random one of those, you’d have to modify that variable to be an array or List and then make sure it’s filled with 3 items in the inspector.
Okay so gave this a try and saved the changes and Unity is still giving me grief.
public class SpawnNukeScript : MonoBehaviour {
public Transform[] spawnPoints;
public GameObject[] randomNuke;
public int startSpawnTime = 10;
public int spawnTime = 5;
// Use this for initialization
void Start () {
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", startSpawnTime, spawnTime);
}
// Update is called once per frame
void Update () {
}
void Spawn () {
// Find a random index between zero and one less than the number of spawn points.
int spawnPoints = Random.Range (0, spawnPoints.Length);
int randomNuke = Random.Range (0, randomNuke.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate(randomNuke,this.spawnPoints[spawnPoints].position, this.spawnPoints[spawnPoints].rotation);
}
}
As you can see from my inspector screen the “Enemy” variable is still there and won’t update to the “randomNuke” array I am trying to se.
okay, well randomNuke is an array and you have to “Instantiate” from the array 's index.
Instantiate(this.randomNuke[randomNuke // etc
As I was saying, it’s much easier to use a diff name for those int variables in Spawn() so you don’t have to prefix your arrays with “this.”
Please try to post the error, when you have it, too. And read the error to yourself, which is often/always helpful to figure out what is wrong.
Unity is giving me grief, for example, is not a helpful remark lol
Unity wasn’t actually giving me an error which is why I used that line, but thank you got the script working with the following:
public class SpawnNukeScript : MonoBehaviour {
public Transform[] spawnPoints;
public GameObject[] randomNuke;
public int startSpawnTime = 10;
public int spawnTime = 5;
// Use this for initialization
void Start () {
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", startSpawnTime, spawnTime);
}
// Update is called once per frame
void Update () {
}
void Spawn () {
// Find a random index between zero and one less than the number of spawn points.
int spawnPoints = Random.Range (0, 5);
int randomNuke = Random.Range (0, 3);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate(this.randomNuke[randomNuke],this.spawnPoints[spawnPoints].position, this.spawnPoints[spawnPoints].rotation);
}
}