Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zach Meyers · Feb 09, 2011 at 11:36 PM · errorobject

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&lt;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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 09, 2011 at 11:43 PM

I think this line:

aGrid[1] = new Array();

should be

aGrid[i] = new Array();
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zach Meyers · Feb 09, 2011 at 11:48 PM 0
Share

Wow... i cannot believe it. That worked. Lawl. I would have never seen that without someone else's eyes looking. Thank. It worked perfectly!

avatar image
-1

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 ) ;

} } } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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 ) ;

} } } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Instantiate GameObjects, brings an error to the log... 2 Answers

Unity 2018.2.0f2 Crashing to Desktop with "Object is already loaded" errors. 2 Answers

i can only see objects in game mode not edit mode, why? 0 Answers

Operator '+' cannot be used 1 Answer

Destroyed Object Error 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges