Randomly instantiate objects of an array, but each only once. The Object you want to instantiate is null.

Hello, I’m kind of new to Unity and here I just can’t figure out why it gives this error in InstObstacles().

In this I am trying to instantiate groups of obstacles.

Per island I want to instantiate at most one obstacle of the same group, thats why I try to set the whole group to null and then let a non-null object be chosen for instantiation. But the only object being instantiated is always the one in SetUp().

Here is the relevant code if something is unclear I will provide more information.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroupSpawner : MonoBehaviour
{
float yy;
int obstaclesAmount;
int randomDimension;
int randomNumber;
int noLoop;

public GameObject a1;
public GameObject a2;
public GameObject a3;
public GameObject b1;
public GameObject b2;
public GameObject b3;
public GameObject c1;
public GameObject d1;
public GameObject e1;
public GameObject f1;
public GameObject g1;

GameObject[,] Obstacles;

private void Awake()
{
    Obstacles = new GameObject[7,3];
}

void Start()
{
    yy = yy + 10;
}

void Update()
{
    SpawnGroups();
}

public void SpawnGroups() //set the positon and amount of obstacle spawn, call Instantiate
{
    if ((yy - gameObject.transform.position.y) < 50)
    {
        float distance = Random.Range(3, 13);   //distance between the groups
        obstaclesAmount = Random.Range(1, 4);   //obstacles per group
        yy = yy + distance;
        SetUp();
    }
}
void SetUp() //include all objects, choose and instantiate random, call Instisland
{
    GameObject[,] Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };
    randomDimension = Random.Range(0, 7);
    randomNumber = Random.Range(0,3);

    Object.Instantiate(Obstacles[randomDimension, randomNumber], new Vector3(lw.transform.position.x + 0.7f, yy, lw.transform.position.z), Quaternion.identity);
    //This gets instantiated fine
    InstObstacles();
}
void InstObstacles() //exclude already spawned obstacles, choose obstacle and instantiate it, repeat x times
{
    Obstacles[randomDimension, 0] = null; Obstacles[randomDimension, 1] = null; Obstacles[randomDimension, 2] = null;
    

    randomDimension = Random.Range(0,7);
    randomNumber = Random.Range(0,3);

    Debug.Log("test " + Obstacles[1,1] + Obstacles[2,0] + Obstacles[4,0] + Obstacles[6,0]);
    //this writes just "test" in the log, which means these are all null right?

    noLoop = 100;

    while (Obstacles[randomDimension,randomNumber] == null)
    {
        randomDimension = Random.Range(0, 7);
        randomNumber = Random.Range(0, 3);

        noLoop--;
        if (noLoop == 1)
        {
            Debug.Log("infiniteWhileLoop"); //gets called every time
            break;
        }
    }

    Debug.Log(Obstacles[randomDimension, randomNumber]);
    //This writes "Null" tho

    yy = yy + 1;
//this gives the error
    Object.Instantiate(Obstacles[randomDimension, randomNumber], new Vector3(lw.transform.position.x + 0.7f, yy, lw.transform.position.z), Quaternion.identity);

    if (obstaclesAmount > 1) //it should call the function again to instantiate another obstacle for the group if true
    {
        obstaclesAmount--;
        InstObstacles();
    }
}
}

Hello @Mike_Honcho,
I’m no expert in C# so sorry if i’m wrong, but didnt you :

  1. declare Obstacles array in your class

  2. declared a 2nd Obstacles array and assigned your gameObjects in it in SetUp

  3. Use Obstacles array declared in you class in IntsObstacles(), which has not been assigned

What happens if you remove the declaration in SetUp ?

Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };