- Home /
The question is answered, right answer was accepted
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();
}
}
}
What's unclear is if/how all these variables get assigned (or even where they're declared)
{ a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { f2, f2, f2 }
The most obvious diagnosis is that they're all null, so all the elements in your array are null...
They are actual objects being set in the editor. I updated the code and added more comments for better understanding.
im having issues to understand your code, obstacles and islands are the same? also the switch is no needed at all and can be changed to:
Obstacles[randomDimension, 0] = null; Obstacles[randomDimension, 1] = null; Obstacles[randomDimension, 2] = null;
try changing the break from iunside the
if(noLoop == 1)
to a
return;
since thats probably giving you the error(altough there might be another bug hard to tell without trying it out)
I refined the code now and your version ins$$anonymous$$d of the switch statement is way better, but that doesn’t change anything. The "noLoop" is just there to avoid unity being stuck in the loop forever. An island (changed to "group" now) should be a group of obstacles in one place.
Can't post as answer atm because question is awaiting moderation, but I think I spotted the error.
At line 55 you declare a local variable. So you never actually populate the Obstacles
member variable which you use in the InstObstacles() function. The local variable can be used within the SetUp() function (which is why line 58 works), but nowhere else.
But I can't just write Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };
ins$$anonymous$$d.
Sure, but this should work...
Obstacles =new GameObject[7,3]{ { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };
(and the initialisation in Awake() becomes superfluous)
Answer by Meishin · May 21, 2019 at 08:20 AM
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 } };
Read the comments. The OP has already been told this, tried your fix, replied that it doesn't work, and been provided with an alternative.
Ay yeah sorry i'm new to the forum i didnt see those comments :)
Follow this Question
Related Questions
How do I access a script from a class inside a script. 1 Answer
Object disappears on running 1 Answer
How to give player a "dash" ability? 0 Answers
How to fix this Score UI problem 2 Answers