- Home /
Randomizing a selection from an array
I am trying to randomize the selection of 3 objects that get displayed on a computer, when running either code I am left with the dreaded "Object reference not set to an instance of an object." the objects are most certainly being found as without the array the items instantiate fine, but with an array I receive this error.
I have tried both this..
void addSale()
{
count++;
dummyTimer = addTimer;
rand = Random.Range(0, adArray.Length);
GameObject temp = Instantiate(adArray[rand], sales.transform);
}
and this..
void addSale()
{
count++;
dummyTimer = addTimer;
GameObject temp = Instantiate(adArray[Random.Range(0, adArray.Length)], sales.transform);
}
and this...
void addSale()
{
count++;
dummyTimer = addTimer;
int arrayLength = adArray.Length;
rand = Random.Range(0, arrayLength);
GameObject temp = Instantiate(adArray[rand], sales.transform);
}
With exact results...
Again. these objects Instantiate fine without an array.... meaning that unity can find these objects without fail... untill I add the array, thats when unity can no longer reference the object that is trying to be instantiated, also the count is being added and the timer reset upon receiving the error.
Answer by Link17x · Mar 16, 2020 at 01:41 AM
So you've narrowed the problem down to the array, and it's producing a null reference exception. I can't tell the problem from the code, because I can't see the array.
Based on what you've said, the only suggestion I can really make is that you might not have initialized your array before using it. You must declare an array using the new keyword before you fill or access it. Have you done this?
If the answer is yes, then the array elements must be null. This means that you're probably not filling the array elements up properly, again, seeing the code for this would be useful to determine this. Since you haven't, I'd suggest making a foreach loop, go over your array and do a Debug.Log on each element to print the contents. Then you can confirm your array contains what you expect.
Hi Link17x, thank you for your answer, I dont understand what you mean by initializing the array before using, below I have put the entire script asnd a photo of the array.
EDIT: the problem was the Instantiate parent was not initialized.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class saleGenerator : $$anonymous$$onoBehaviour
{
[SerializeField]
GameObject[] adArray;
GameObject sales; ///////Error was here.. I had not initialized the transform that the instantiated prefabs spawn to..
float addTimer = 20f;
public float dummyTimer;
public int count;
public int rand;
void Start()
{
dummyTimer = addTimer;
sales = gameObject;
}
void Update()
{
if(dummyTimer > 0)
{
dummyTimer -= Time.deltaTime;
}
if(dummyTimer <= 0)
{
addSale();
}
}
void addSale()
{
count++;
dummyTimer = addTimer;
int arrayLength = adArray.Length;
rand = Random.Range(0, arrayLength);
GameObject temp = Instantiate(adArray[rand], sales.transform);
}
}
What has happened to me is that sometimes such components are also attached to another object by accident and there your array has nothing. Can you double check and make sure no other object has this component on the scene
Hi thanks for the comment, however I have already fixed this, the prefabs and the instantiate side of things were fine, but the object i wished to parent the newly instantiated objects I had forgotten to initialize in the start funtion