- Home /
My array of images is null for some reason?
I have an array of images to display cards in the players hand. It doesn't work, I get an error saying that the object reference is not set to an instance of an object.
public int health;
public int mana;
public bool myTurn;
private Card[] deck;
private int cardsInHand;
public Image[] hand;
private PlayerCards playerCards;
// Use this for initialization
public void startGame(PlayerCards pc) {
playerCards = pc;
cardsInHand = 0;
health = 30;
mana = 0;
deck = playerCards.getDeck();
beginTurn();
}
public void Draw(int num)
{
for (int i = 0; i < num; i++)
{
//Draw a card
Card card = playerCards.draw();
//show an image
hand[0].enabled = true;
cardsInHand++;
}
}
Your hierarchy seems to be reddish. That could mean that it is a prefab and you deleted that prefab from the project folder.
Have you check those cards if they appear if you enable it in the inspector?
Yeah, I did delete the prefab. I don't think that is the issue, I only made the prefab to see if that would help :p
@jchester07 I've updated it all to use prefabs of game objects. I still get the same weird error.
hand[cardsInHand].active = true; which I know is deprecated, but thats not the issue here.
Answer by deanpackard · Dec 06, 2017 at 02:36 AM
I figured it out. It had stuff to do with initialization in this function of another class
void Start () {
playerCards = new PlayerCards();
brodeCards = new BrodeCards();
brodeCards.startGame();
playerCards.startGame();
pc.myFirstTurn = true;
}
Before I wasn't getting an object reference to my player controller (pc), and so it wasn't running properly, its all good now though! Thanks everyone for your help.
Answer by Kwergan · Dec 06, 2017 at 02:06 AM
That particular error will occur usually if you forgot to assign the public variables with a value. Check in the inspector that you have assigned the deck and hand arrays.
I did, but I may have missed something, look at the comments above :)
Anywhere in your code where you replace, remove, add items to the array? Arrays are not really scaleable, so the number you assign in the inspector is fixed. You can modify, but not add items. (Practically speaking) @deanpackard
Your answer
