Array index is out of range error - c#

I'm using this code to spawn a random object from an array:
using UnityEngine;
using System.Collections;
public class enemySpawner : MonoBehaviour {
public GameObject[] enemies;
int enemyNo;
public float maxPos = 6.9f;
public float delayTimer = 0.75f;
float timer;
void Start () {
timer = delayTimer;
}
void Update () {
timer -= Time.deltaTime;
if (timer <= 0) {
Vector3 enemyPos = new Vector3 (transform.position.x, Random.Range (5.0f, -5.5f), transform.position.z);
//enemyNo = Random.Range (0,8);
enemyNo = Random.Range (0, enemies.Length);
Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
timer = delayTimer;
}
}
}
The problem is I want to do the same thing across different scenes. Each scene has a different amount of objects for the array (set in the inspector), so because they're not all the same I'm getting this error:
IndexOutOfRangeException: Array index is out of range.
Is there any way for me to do this differently? Or should I write a new script for each scene?

You need to get the current length of the array, so you can't get out of the current array range.
enemyNo = Random.Range (0, enemies.Length)

Related

how can I set an amount of game objects to spawn?

here is my code and i have no idea how to work it up. im a beginner in c# and this is for our project.
the project is about a balloon popper and i am having trouble to set an amount of balloons to spawn.
I am planning to set the amount of balloons to spawn at 5, 10 even 20 and after that, the spawning will stop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnScript: MonoBehaviour
{
public Transform[] spawnPoints;
public GameObject[] balloons;
public float spawnTime = 0f;
float spawnTimeLeft = 0f;
// Start is called before the first frame update
void Update()
{
if (spawnTimeLeft >= spawnTime)
{
int randBalloon = Random.Range(0, balloons.Length);
int randSpawnPoint = Random.Range(0, spawnPoints.Length);
Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
spawnTimeLeft = 0f;
}
else
{
spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
}
}
}
Add a variable to the class for the maximum amount. Have done this as public so you can set it in the inspector to the amount you wish.
Also add a counter variable and set it to 0. This is private as nothing but this class needs access to it. Do serialize this field.
public int spawnAmountMax = 5;
[SerializeField]
private int spawned = 0;
Start the Update() with the next check, return from the method when it evaluates to true.
if (spawned >= spawnAmountMax)
return;
Than after instantiating the GameObject, increase the variable:
spawned++;
The adjusted code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
public class SpawnScript : MonoBehaviour
{
public int spawnAmountMax = 5;
private int spawned = 0;
public Transform[] spawnPoints;
public GameObject[] balloons;
public float spawnTime = 0f;
float spawnTimeLeft = 0f;
// Start is called before the first frame update
void Update()
{
if (spawned >= spawnAmountMax)
return;
if (spawnTimeLeft >= spawnTime)
{
int randBalloon = Random.Range(0, balloons.Length);
int randSpawnPoint = Random.Range(0, spawnPoints.Length);
Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
spawnTimeLeft = 0f;
spawned++;
} else
{
spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
}
}
}
Try this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnScript: MonoBehaviour
{
public Transform[] spawnPoints;
public GameObject[] balloons;
public float spawnTime = 5.0f;
public float maxSpawnTime = 20.0f;
private void Start()
{
Invoke(nameof(Spawn), spawnTime); //This will start the spawning process after the initially set spawnTime, so first this will wait for 5 seconds.
}
private void Spawn()
{
int randBalloon = Random.Range(0, balloons.Length);
int randSpawnPoint = Random.Range(0, spawnPoints.Length);
Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
spawnTime *= 2; //doubles the spawnTime, according to your request, so the second time this will be 10, and the third time, this will be 20 seconds
if (spawnTime <= maxSpawnTime) //check if we reached or not the maximum spawn time
{
Invoke(nameof(Spawn), spawnTime); //if we not yet reached the maximum spawn time, it will start to wait and spawn one more balloon again.
}
}
}
since you're doing this in you update, you can add a counter after your Instantiate call.
Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
counter++;
And then, nest your if statement inside another if checking for the counter.
if(counter < amountToSpawn)
{
//your code here
}
For me personally, I would make a Coroutine method with delay that will have a for loop inside, rather than doing this in update.
Sample:
private IEnumerator SpawnObjects(int spawnCount)
{
for(int i = 0; i < spawnCount; i++)
{
//Instantiate here
yield return new WaitForSeconds(5);
}
}
To call the method:
StartCoroutine(SpawnObjects(10));

Unity3d scripting error c# IndexOutOfRangeException: Array index is out of range

This is my spawning script belowwritten in c# The script should create objects randomly in the scene.
The issue is that I'm getting this error at runtime.
IndexOutOfRangeException: Array index is out of range.
CreateEasterEggs.MakeThingToSpawn () (at Assets/CreateEasterEggs.cs:52)
CreateEasterEggs.Update () (at Assets/CreateEasterEggs.cs:28)
Not sure what I have done wrong, thinking its something to do with the game object?
Thank you.
using UnityEngine;
using System.Collections;
public class CreateEasterEggs : MonoBehaviour
{
public float secondsBetweenSpawning = 0.1f;
public float xMinRange = -25.0f;
public float xMaxRange = 25.0f;
public float yMinRange = -5.0f;
public float yMaxRange = 0.0f;
public float zMinRange = -25.0f;
public float zMaxRange = 25.0f;
public GameObject[] spawnObjects; // what prefabs to spawn
private float nextSpawnTime;
void Start ()
{
// determine when to spawn the next object
nextSpawnTime = Time.time+secondsBetweenSpawning;
}
void Update ()
{
// if time to spawn a new game object
if (Time.time >= nextSpawnTime) {
// Spawn the game object through function below
MakeThingToSpawn ();
// determine the next time to spawn the object
nextSpawnTime = Time.time+secondsBetweenSpawning;
}
}
void MakeThingToSpawn ()
{
//Start the vector at an invalid position
Vector3 spawnPosition = new Vector3(0, 0, 0);
//while we are not in the right range, continually regenerate the position
while ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4))
{
spawnPosition.x = Random.Range (xMinRange, xMaxRange);
spawnPosition.y = Random.Range (yMinRange, yMaxRange);
spawnPosition.z = Random.Range (zMinRange, zMaxRange);
}
// determine which object to spawn
int objectToSpawn = Random.Range (0, spawnObjects.Length);
// actually spawn the game object
GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;
// make the parent the spawner so hierarchy doesn't get super messy
spawnedObject.transform.parent = gameObject.transform;
}
}
IndexOutOfRange means that you tried to access to an element of an array that doesn't exist.
In your case as you are doing it with Random.Range (0, spawnObjects.Length); Then the only possible case is that your array is empty.
Try to Debug.Log(spawnObjects.Length): before the Instantiate and you will see that in fact your array of gameobjects is empty as It will return 0.

Unity: Overlapping spawn Random Objects at Random Positions in C#

I am trying to develop a 2d game where my enemies will pop-up and disappear in random position without overlapping with each other. I am following this tutorial in youtube https://www.youtube.com/watch?v=iLTP4EbM1N4 but when my player touch a enemy then he is losing 2 lives instead of 1. I think it's because enemies randomly spawn in the same spawnpoint (I have 5 enemies). Do you have any suggestion how to fix that? Any help would be highly appreciated. Thanks in advance. Here is my two scripts:
SpawnItems.cs
using UnityEngine;
using System.Collections;
public class SpawnItems : MonoBehaviour
{
public Transform[] SpawnPoints;
public float spawnTime = .5f;
public GameObject[] Coins;
void Start ()
{
InvokeRepeating ("SpawnCoins", spawnTime, spawnTime);
}
void Update ()
{
}
void SpawnCoins()
{
for (int i = 0; i < Coins.Length; i++) {
int spawnIndex = Random.Range (0, SpawnPoints.Length);
//int objectIndex = Random.Range (0, Coins.Length);
Instantiate (Coins [i], SpawnPoints [spawnIndex].position, SpawnPoints [spawnIndex].rotation);
}
}
Destroy.cs
using UnityEngine;
using System.Collections;
public class DestoryScript: MonoBehaviour
{
public float destoryTime = .5f;
private float rotateSpeed = 300.0f;
void Start ()
{
Destroy (gameObject, destoryTime);
}
void Update ()
{
transform.Rotate (Vector3.forward * Time.deltaTime * rotateSpeed);
}
}
Orange ball overlaps with green ball
Your SpawnCoins() in SpawnItems.cs should be like:
void SpawnCoins()
{
Transform currentSpawnPoint = SpawnPoints[Random.Range(0, SpawnPoints.Length)].transform;
Instantiate (Coins [Random.Range(0, Coins.Length)], currentSpawnPoint.position, currentSpawnPoint.rotation);
}

spawn gameobject from array

I'm having some trouble spawning a random asteroid from an array within a for loop.
The array is working now, however (I assume this is in the for loop somewhere) it wont alternate between the different game objects. whichever object spawns first, spawns every time, but each time you load the game it spawns a different one every time.
How do I get it to chose a new random object from the array after every spawn instance?
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject[] asteroids;
public Vector3 spawnValues;
public int asteroidCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Start () {
StartCoroutine (spawnWaves ());
}
IEnumerator spawnWaves () {
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];
while (asteroidCount > 0) {
for (int i = 0; i < asteroidCount; i++) {
Vector3 spawnPosition = new Vector3 (spawnValues.x, Random.Range (-spawnValues.y, spawnValues.y), spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (asteroid, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
}
you select the object to spawn outside the loop
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];
needs to be here
while (asteroidCount > 0) {
for (int i = 0; i < asteroidCount; i++) {
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];

How to spawn enemy at random intervals?

I am using EnemyScript to move the enemy towards the player and killing the player, but I'm unable to spawn it randomly in code. I am currently spawning it directly through screen by placing the prefab on the scene.
Here is my EnemyScript
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
public Transform target;
public float speed = 2f;
void Update ()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D otherCollider)
{
PlayerControl shot = otherCollider.gameObject.GetComponent<PlayerControl>();
if (shot != null)
{
Destroy(shot.gameObject);
}
}
}
you could use something similar to this:
public GameObject myObj;
void Start ()
{
enemy = GameObject.Find("enemy");
InvokeRepeating("SpawnEnemy", 1.6F, 1F);
}
public void SpawnEnemy()
{
Vector3 position = new Vector3(Random.Range(35.0F, 40.0F), Random.Range(-4F, 2F), 0);
Instantiate(myObj, position, Quaternion.identity);
}
in the InvokeRepeating call you could possibly add the random range there also instead of a timed instantiate. This example is just a snippet of some prototype code i did a while ago, it may not suit your needs directly but hopefully will give you a general idea on what to do.
EDIT: to make sense, put this into a blank object somewhere in your scene, dont attach this to the actual enemy.
i programmed it like this.
public GameObject enemyPrefab;
public float numEnemies;
public float xMin = 20F;
public float xMax = 85F;
public float yMin = 3.5F;
public float yMax = -4.5F;
void Start () {
for (int i=0; i< numEnemies; i++) {
Vector3 newPos = new Vector3(Random.Range(xMin,xMax),Random.Range(yMin,yMax),0);
GameObject enemy = Instantiate(enemyPrefab,newPos,Quaternion.identity) as GameObject;
}
}
This will spawn a random number of enemies, at random locations, after random periods of time, all adjustable in the Inspector.
public float minTime = 1;
public float maxTime = 3;
public int minSpawn = 1;
public int maxSpawn = 4;
public Bounds spawnArea; //set these bounds in the inspector
public GameObject enemyPrefab;
private spawnTimer = 0;
Vector3 randomWithinBounds(Bounds r) {
return new Vector3(
Random.Range(r.min.x, r.max.x),
Random.Range(r.min.y, r.max.y),
Random.Range(r.min.z, r.max.z));
}
void Update() {
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0) {
spawnTimer += Random.Range(minTime, maxTime);
int randomSpawnCount = Random.Range(minSpawn, maxSpawn);
for(int i = 0; i < randomSpawnCount; i++) {
Instantiate(transform.transformPoint(enemyPrefab), randomWithinBounds(spawnArea), Quaternion.identity);
}
}
}
//bonus: this will show you the spawn area in the editor
void OnDrawGizmos() {
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.color = Color.yellow;
Gizmos.drawWireCube(spawnArea.center, spawnArea.size);
}
People on here seem to like using coroutines for time delays, but I personally prefer to track my own timers in Update() to maximize control and predictability.

Categories

Resources