Need help pinning down NullReference exception, please!

So I’m pretty sure I’m just misunderstanding how part of my code is working right now, or there might be a normal old Null Reference somewhere that I’m not seeing.

When I call DrawCard() the Null Reference exception is thrown.

	//	Draw the top card of the deck.
	public void DrawCard () {

		Card c = CreateInstance<Card>();
		c = library.RemoveCard(0);
		hand.AddCard(c);
		Debug.Log ("Cards in deck: "+library.cardsInLibrary+" Cards in hand: "+hand.cardsInHand);
	}

Hand Class:

public class Hand : ScriptableObject {

	public int maxHandSize = 7;
	public int cardsInHand;
	public Card[] contents;
	
	void Start () {

		LoadHand ();
	}

	void Update () {
	
	}

	void LoadHand () {

		for(int i=0; i<maxHandSize; i++){

			contents *= CreateInstance<Card>();*
  •  }*
    
  • }*

  • public void AddCard (Card c) {*

  •  Card[] a = new Card[cardsInHand+1];*
    
  •  for(int i=0; i<cardsInHand+1; i++){*
    
  •  	if(i==cardsInHand)*
    

a = contents*;*
* if(i==cardsInHand+1)*
_ a = c;
* }*_

* contents = a;*
* cardsInHand++;*
* }*
}
Deck Class:
public class Deck : ScriptableObject {

* public int deckSize = 60;*
* public int cardsInLibrary;*
* public Card[] contents;*

* void Start () {*

* LoadDeck ();*
* ShuffleDeck ();*
* }*

* void Update () {*

* }*

* // Creates the deck contents array and populates it.*
* void LoadDeck(){*

* contents = new Card[deckSize];*
* cardsInLibrary = contents.Length;*

* for(int i=0; i<deckSize; i++){*

_ contents = CreateInstance();
* }*_

* // Temporarily assigns the Cards in the array with names.*
* for(int i=0; i<deckSize; i++){*

_ contents*.cardName = “Card #”+(i+1);
}
}*_

* // Removes the c position card in the Deck contents and returns the removed card.*
* public Card RemoveCard(int n){*

* Card removedCard = new Card();*
* Card[] a = new Card[cardsInLibrary-1];*

* for(int i=0; i<cardsInLibrary-1; i++){*

* if(i!=n){*
a = contents*;*
* }*

* if(i==n){*
_ removedCard = a*;
}
}*_

* contents = a;*
* cardsInLibrary–;*
* return removedCard;*
* }*

* //Randomize the contents array.*
* public void ShuffleDeck(){*

* for(int i=0; i<deckSize; i++){*

* //int r = Random.Range (1, 60);*
* }*
* }*
}
Player Class:
public class Player : ScriptableObject {

* public string playerName;*
* public bool isOnPlay;*
* public Phases currentPhase;*
* public int life;*
* public int playerTurn;*

* // GAME ZONES*
* public Deck library;*
* public Hand hand;*
* public Graveyard graveyard;*
* public Exile exile;*

* // DEFINE THE PHASES OF A TURN*
* public enum Phases{*

* Untap,*
* Upkeep,*
* Draw,*
* Main1,*
* DeclareAttackers,*
* DeclareBlockers,*
* FirstStrikeCombatDamage,*
* CombatDamage,*
* Main2,*
* End,*
* Cleanup*
* }*

* void Start () {*

* library = CreateInstance();*
* hand = CreateInstance();*
* }*

* void Update () {*

* }*

* // Draw the top card of the deck.*
* public void DrawCard () {*

* Card c = CreateInstance();*
* c = library.RemoveCard(0);*
* hand.AddCard(c);*
* Debug.Log ("Cards in deck: “+library.cardsInLibrary+” Cards in hand: "+hand.cardsInHand);*
* }*
}

You do a lot strange things here :wink:

  • ScriptableObject doesn’t have a Start or Update callback. It only has OnEnable, OnDisable and OnDestroy. Those are more like your constructor and destructor. There are no other callbacks. It’s just like a normal class but it can be stored as asset.

  • Your AddCard method looks really strange. First of all your two if statements are the same while i guess the first one should be the opposite : *#1 See below.

  • Regarding your Hand and Deck class, it would be way easier when you use a generic List instead of an array since it already has an Add and Remove method.

  • Your RemoveCard method is also wrong. You are supposed to move all elements which come after the removed index one index up. Again use a List or if you want to do it yourself, draw it on a sheet of paper.

    //#1*:

    if( i < cardsInHand )

ps: Had to move the code line down here since UA is screwing up inline code blocks again due to the “<”