- Home /
Boo: Object reference not set to an instance of an object
Hey Guys, I tried to use multidimensional arrays in boo and it just threw this error:
NullReferenceException: Object reference not set to an instance of an object
GameScript.buildGrid () (at Assets/Scripts/GameScript.boo:28)
GameScript.OnGUI () (at Assets/Scripts/GameScript.boo:40)
Here's my Code:
import UnityEngine
public class Card (System.Object):
isFaceUp as bool = false
isMatched as bool = false
public img = "robot"
class GameScript (MonoBehaviour):
cols as int = 4
rows as int = 4
totalCards as int = cols * rows
matchesNeededToWin as int = totalCards * 0.5
mathesMade as int = 0
cardW as int = 100
cardH as int = 100
aCards as List
aGrid as (Card, 2)
aCardsFlipped as List
playerCanClick as bool
playerHasWon as bool = false
def buildGrid():
GUILayout.BeginVertical()
for i in range(rows):
GUILayout.BeginHorizontal()
for j in range(cols):
card = aGrid[i,j]
if (GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW))):
Debug.Log(card.img)
def Start ():
playerCanClick = true
for i in range(rows):
for j in range(cols):
aGrid[i,j] = Card()
def OnGUI ():
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height))
//buildGrid()
GUILayout.EndArea()
Thanks in advance!
Wow, that's the first time I've seen a boo script. Are you absolutely sure you want to use this language?
Answer by Landern · Aug 29, 2014 at 12:54 PM
You made the size of 2 for aGrid of type Card.
aGrid as (Card, 2)
Your rows and cols are 4 each.
cols as int = 4
rows as int = 4
The for loop is using range(4) for both rows and cols.
for i in range(rows):
GUILayout.BeginHorizontal()
for j in range(cols):
card = aGrid[i,j]
aGrid doesn't have the range you're looking for.
disclaimer: haven't touched boo in years.
Thanks for your answer! This could be the problem, but I don't know how to create 2- dimensional arrays in this language. This page describes that you can create a 2-dimensional array like that: aGrid as (Card, 2), but it didn't work. So if I would use aGrid as (Card, 4) it would create an 4-dimensional array?? (excuse me for my bad english, I'm still learning)