In my game, my Player GameObject will hold four cards, which are also GameObjects with a CardObject script attached:
CardObject.cs
using UnityEngine;
using System.Collections;
public class CardObject : MonoBehaviour
{
public int value;
public int color;
}
The Player object accepts a card prefab as a public variable. I then clone four cards and keep reference to them in a List, like so:
PlayerObject.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerObject : MonoBehaviour
{
public GameObject card;
private List<CardObject> cards;
void Start()
{
GenerateCards();
}
void GenerateCards()
{
cards = new List<CardObject>();
for (int i = 0; i < 4; i++)
{
var c = Instantiate(card, Vector2.zero, Quaternion.identity) as CardObject;
c.value = Random.Range(1, 9);
c.color = Random.Range(0, 2);
cards.Add(c);
}
}
}
First of all, I’m getting a non-fatal error:
NullReferenceException: Object reference not set to an instance of an object
PlayerObject.GenerateCards () (at Assets/Scripts/PlayerObject.cs:26)
PlayerObject.Start () (at Assets/Scripts/PlayerObject.cs:16)
But I’m not sure why.
Secondly, if you were implementing similar, would you recommend this method? I am particularly interested in the best way to generate a lot of random content at runtime, and load it into the scene. One of the problems I will face is how to reference each created object? Presumably storing them in a list I can then reference later is the way, but the above error seems to suggest I’m doing it wrong.