- Home /
Script wont work... Keep getting an Object not set to an instance error
Hey Guys,
Im pretty new to Unity and scripting in general. I purchased a book that lays out some code for a memory game called "Robot Repair". I will past the script so you guys can see it. The error seems to be coming from inside the function BuildGrid() where it declares a variable card and sets it to Object.
var cols:int = 4; var rows:int = 4; var totalCards:int = cols*rows; var matchesNeededToWin:int = totalCards * .5; var matchesMade:int = 0; var cardW:int = 100; var cardH:int = 100; var aCards:Array; var aGrid:Array; var aCardsFlipped:ArrayList; var playerCanClick:boolean; var playerHasWon:boolean = false;
function Start() { playerCanClick = true;
aCards = new Array(); aGrid = new Array(); aCardsFlipped = new ArrayList();
for(i=0; i<rows; i++) { aGrid[1] = new Array();
for(j=0; j<cols; j++)
{
aGrid[i][j] = new Card();
}
}
} function OnGUI () { GUILayout.BeginArea (Rect(0,0,Screen.width, Screen.height)); BuildGrid(); GUILayout.EndArea(); print("building grid!"); } class Card extends System.Object { var isFaceUp:boolean = false; var isMatched:boolean = false; var img:String;
function Card()
{
img = "robot";
}
}
function BuildGrid() { GUILayout.BeginVertical(); for (i=0; i
Answer by Bunny83 · Feb 09, 2011 at 11:43 PM
I think this line:
aGrid[1] = new Array();
should be
aGrid[i] = new Array();
Wow... i cannot believe it. That worked. Lawl. I would have never seen that without someone else's eyes looking. Thank. It worked perfectly!
Answer by beavis1230 · Aug 13, 2012 at 06:02 AM
////////////////////////////////////////////////////////////// // Purpose: This class will setup the Arrays neccessary for // cards to be displayed on the screen. These cards // have no functionality, and just being able to see // the cards will complete this lab // // Grade: 100/100 //////////////////////////////////////////////////////////////
class CardArraySetup { /////////////////////////////////////////////////////////////////////// // Singleton setup so that this class can be used throughout the // project private static var instance : CardArraySetup = new CardArraySetup();
public static function GetInstance() : CardArraySetup { return instance; } ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////// // NOTE: You will be using the variables here for rows and column when // you are creating the multi-dimensional array later in this lab private var rows : int = GameScript.rows; private var cols : int = GameScript.cols; /////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////// // TODO (20 points) // You are going to have to create 3 different types of arrays ////////////////////////////////////////////////////////////////////////////////////// // Code Explanation: // The first two are going to be Javascript arrays, while the last one // will be an ArrayList. // One of the Javascript arrays will be used for storing the total number of cards // available in this game // The second array will be used to form the multi-dimensional grid later on in this // lab. // The final variable, the ArrayList will be holding only two cards at a time, // the cards that were actually flipped. //////////////////////////////////////////////////////////////////////////////////////
// Declare two new Arrays and one new ArrayList here. Make sure you name them appropriate // to what they will be used for. Make sure they are PRIVATE variable declarations, scope them // in correctly, and create them as new empty declarations. This means you will need // to set them equal to new empty Arrays.
// Type your 3 variable declarations right below here:
private var RoboCard : Array = new Array ( ) ;
private var GridRunner : Array = new Array ( ) ;
private var HeelFlips : ArrayList = new ArrayList ( ) ;
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// // Code Explanation // Accessor function to this class // In order to be used elsewhere in the code, and to follow with proper object oriented // principles, we do not want the user to have direct access to the code. So instead, we use // functions called Accessors to allow for other portions of the code to have access to the // variables you have created.
// This means that you will be using functions that send your variable to be used elsewhere. // One way to do this is to return data from a function. Next lab you will be going over functions // and returns, but with your next todo you need to give access to the variables you created above // using the return keyword, giving other code found in this project access to the arrays ////////////////////////////////////////////////////////////////////////////////////////////////
// Access to the arrays
function GetCardArray() : Array { // TODO (5 points) // You will need to return the Array that you declared above // using the return keyword and then the name of the array representing // the array holding the total number of cards
return RoboCard ;
}
function GetGridArray() : Array { // TODO (5 points) // You will need to return the Array you declared above // using the return keyword and then the name of the Array representing // the multi-dimensional grid that will be created later //return grid :
return GridRunner; }
function GetCardsFlippedArray() : ArrayList { // TODO (5 points) // You will need to return the arraylist that you declared above // using the return keyword and the name of the ArrayList representing // the two cards that have been flipped over
return HeelFlips ; }
function ResetCardsFlipped() {
// TODO (5 points) // You are going to need to reset your arraylist that you declared above. // In order to reset the arraylist, if you set the variable to a new ArrayList // again, without needing to re-declare it, it will erase whatever data is found // inside of the ArrayList. Remember, do not give it a type, or precede it with a // var keyword for this part.
HeelFlips = new ArrayList ();
}
///////////////////////////////////////////////////////////////////////////////////// // Code Explanation: // This function is going to setup the grid array you created earlier to be // multi-dimensional and will be using two for loops to create both rows and // columns for the grid // TIP: This is VERY similar to the lecture notes found for Arrays, and using those // will be helpful in completing this lab!
function GridSetup() {
// TODO (20 points) // Create a multi-dimensional array here to hold the cards for the grid. It should loop // through the rows and columns sizes given to you at the top of the file. Use the array designated // for the grid you declared above.
for ( var x : int = 0 ; x < rows ; x ++) {
GridRunner [ x ] = new Array ( );
for ( var y : int = 0; y < cols ; y ++) {
// HINT: Check the lecture notes to see a similar example of setting up a multi-dimensional // array // Now within the second nested for loop you need to perform a few other operations found below: ///////////////////////////////////////////////////////////////////// // Uncomment this line of code, but replace the <> with the array // name for your total card array you created above. // Make sure the dot operator (.) remain!! // This line of code will store a random number into the variable someNum
////////////////////////////////////////////////////////////////////// // Code Explanation: // Still within this nested for loop, you need to perform two more // actions using the random number created above that you // commented back in. This random number creates an integer between // 0 and the total number of cards in your total card array
var someNum : int = Random . Range ( 0 , RoboCard . length ) ;
// TODO (20 points) // A) The first step is to set the current location of multi-dimensional array equal // to the random index of your total card array using the someNum variable // Use the counter created in both of the for loops to access the current location
GridRunner [ x ] [ y ] = RoboCard [someNum] ;
// TODO (20 points) // B) The second step requires you to remove the information from your total // card array at the same random location. The reason // is so that we can get rid of the card that was set just before so it wont be used // again. // HINT: Use the API scripting reference for Unity 3D and search for Arrays to find // the correct method to perform this action
RoboCard . RemoveAt ( someNum ) ;
} } } }
Answer by beavis1230 · Aug 13, 2012 at 06:02 AM
////////////////////////////////////////////////////////////// // Purpose: This class will setup the Arrays neccessary for // cards to be displayed on the screen. These cards // have no functionality, and just being able to see // the cards will complete this lab // // Grade: 100/100 //////////////////////////////////////////////////////////////
class CardArraySetup { /////////////////////////////////////////////////////////////////////// // Singleton setup so that this class can be used throughout the // project private static var instance : CardArraySetup = new CardArraySetup();
public static function GetInstance() : CardArraySetup { return instance; } ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////// // NOTE: You will be using the variables here for rows and column when // you are creating the multi-dimensional array later in this lab private var rows : int = GameScript.rows; private var cols : int = GameScript.cols; /////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////// // TODO (20 points) // You are going to have to create 3 different types of arrays ////////////////////////////////////////////////////////////////////////////////////// // Code Explanation: // The first two are going to be Javascript arrays, while the last one // will be an ArrayList. // One of the Javascript arrays will be used for storing the total number of cards // available in this game // The second array will be used to form the multi-dimensional grid later on in this // lab. // The final variable, the ArrayList will be holding only two cards at a time, // the cards that were actually flipped. //////////////////////////////////////////////////////////////////////////////////////
// Declare two new Arrays and one new ArrayList here. Make sure you name them appropriate // to what they will be used for. Make sure they are PRIVATE variable declarations, scope them // in correctly, and create them as new empty declarations. This means you will need // to set them equal to new empty Arrays.
// Type your 3 variable declarations right below here:
private var RoboCard : Array = new Array ( ) ;
private var GridRunner : Array = new Array ( ) ;
private var HeelFlips : ArrayList = new ArrayList ( ) ;
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// // Code Explanation // Accessor function to this class // In order to be used elsewhere in the code, and to follow with proper object oriented // principles, we do not want the user to have direct access to the code. So instead, we use // functions called Accessors to allow for other portions of the code to have access to the // variables you have created.
// This means that you will be using functions that send your variable to be used elsewhere. // One way to do this is to return data from a function. Next lab you will be going over functions // and returns, but with your next todo you need to give access to the variables you created above // using the return keyword, giving other code found in this project access to the arrays ////////////////////////////////////////////////////////////////////////////////////////////////
// Access to the arrays
function GetCardArray() : Array { // TODO (5 points) // You will need to return the Array that you declared above // using the return keyword and then the name of the array representing // the array holding the total number of cards
return RoboCard ;
}
function GetGridArray() : Array { // TODO (5 points) // You will need to return the Array you declared above // using the return keyword and then the name of the Array representing // the multi-dimensional grid that will be created later //return grid :
return GridRunner; }
function GetCardsFlippedArray() : ArrayList { // TODO (5 points) // You will need to return the arraylist that you declared above // using the return keyword and the name of the ArrayList representing // the two cards that have been flipped over
return HeelFlips ; }
function ResetCardsFlipped() {
// TODO (5 points) // You are going to need to reset your arraylist that you declared above. // In order to reset the arraylist, if you set the variable to a new ArrayList // again, without needing to re-declare it, it will erase whatever data is found // inside of the ArrayList. Remember, do not give it a type, or precede it with a // var keyword for this part.
HeelFlips = new ArrayList ();
}
///////////////////////////////////////////////////////////////////////////////////// // Code Explanation: // This function is going to setup the grid array you created earlier to be // multi-dimensional and will be using two for loops to create both rows and // columns for the grid // TIP: This is VERY similar to the lecture notes found for Arrays, and using those // will be helpful in completing this lab!
function GridSetup() {
// TODO (20 points) // Create a multi-dimensional array here to hold the cards for the grid. It should loop // through the rows and columns sizes given to you at the top of the file. Use the array designated // for the grid you declared above.
for ( var x : int = 0 ; x < rows ; x ++) {
GridRunner [ x ] = new Array ( );
for ( var y : int = 0; y < cols ; y ++) {
// HINT: Check the lecture notes to see a similar example of setting up a multi-dimensional // array // Now within the second nested for loop you need to perform a few other operations found below: ///////////////////////////////////////////////////////////////////// // Uncomment this line of code, but replace the <> with the array // name for your total card array you created above. // Make sure the dot operator (.) remain!! // This line of code will store a random number into the variable someNum
////////////////////////////////////////////////////////////////////// // Code Explanation: // Still within this nested for loop, you need to perform two more // actions using the random number created above that you // commented back in. This random number creates an integer between // 0 and the total number of cards in your total card array
var someNum : int = Random . Range ( 0 , RoboCard . length ) ;
// TODO (20 points) // A) The first step is to set the current location of multi-dimensional array equal // to the random index of your total card array using the someNum variable // Use the counter created in both of the for loops to access the current location
GridRunner [ x ] [ y ] = RoboCard [someNum] ;
// TODO (20 points) // B) The second step requires you to remove the information from your total // card array at the same random location. The reason // is so that we can get rid of the card that was set just before so it wont be used // again. // HINT: Use the API scripting reference for Unity 3D and search for Arrays to find // the correct method to perform this action
RoboCard . RemoveAt ( someNum ) ;
} } } }