- Home /
Question by
Kellerr · Jun 22, 2014 at 11:05 PM ·
c#gameobjectobjectsinstance
Spawning random objects. Trouble /w objects. Null Reference.
My goal is to spawn a hundred Random gameobjects from a list of prefabs, however i have no clue how to assign said prefabs and looking through the documentation/google have not found an answer. I am likely missing something completely obvious.
class SpawnDiscs : MonoBehaviour
{
public static GameObject[] discArray;
float currentPos = -40f;
float discSpacing = 50f;
void Start()
{
for (int i = 0; i < 100; i++)
{
Disc d = new Disc();
Vector3 pos = new Vector3(currentPos, 0, 0);
d.disc = Disc.setDisc(pos, UnityEngine.Random.Range(0,0));
currentPos -= discSpacing;
}
}
class Disc
{
public GameObject disc = new GameObject();
public static GameObject setDisc(Vector3 Pos, int kind)
{
GameObject go = (GameObject)Instantiate(discArray[UnityEngine.Random.Range(0, discArray.Length)]);
go.name = "Thing";
go.transform.position = Pos;
return go;
}
}
}
On assignment where i instantiate one of those, i get a Null Reference Error: Object reference not set to an instance of object.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613152255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Creating and Moving objects 2 Answers
Multiple Cars not working 1 Answer
Dynamically Changing GameObject makes NullReferenceException(C# Unity) 1 Answer