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 pokobros · Feb 25, 2014 at 10:26 PM · javascriptobjectreferenceargument

Object reference not set to an instance of an object

I am trying to run this code for a game:

 function Start () {
   playerCanClick = true; // We should let the player play, don't you think?
 
   // Initialize some empty Collections:
   aCards = new List.<Card>(); // this Generic List is our deck of cards. It can only ever hold instances of the Card class.
   aGrid = new Card[rows,cols]; // The rows and cols variables help us define the dimensions of this 2D array
   aCardsFlipped = new List.<Card>(); // This List will store the two cards the player flips over.
   BuildDeck();
   // Loop through the total number of rows in our aGrid List:  
   for(var i:int = 0; i<rows; i++)
   {
   
       // For each individual grid row, loop through the total number of columns in the grid:
       for(var j:int = 0; j<cols; j++)
       {
         var someNum:int = Random.Range(0,aCards.Count);
          aGrid[i,j] = aCards[someNum];
          aCards.RemoveAt(someNum);
       }
     }
   }
   
   function OnGUI ()
   {
      GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
      BuildGrid();
      if(playerHasWon)BuildWinPrompt();
      GUILayout.EndArea();
   }
   
 function BuildGrid()
 {
   GUILayout.BeginVertical();
   GUILayout.FlexibleSpace();
   for(var i:int=0; i<rows; i++)
   {
     GUILayout.BeginHorizontal();
     GUILayout.FlexibleSpace();
     for(var j:int=0; j<cols; j++)
     {
       var card:Object = aGrid[i,j]; 
       var img:String;
       
       if(card.isMatched)
       {
            img = "blank";
       } else {
           if(card.isFaceUp)
             {
             img = card.img;
             }else{
             img = "faceup";
             }
         }    
           
       GUI.enabled = !card.isMatched;
       
       if(GUILayout.Button(Resources.Load(img), GUILayout.Width(80), GUILayout.Height(80)))
       {
           if(playerCanClick){
               FlipCardFaceUp(card);
       }
         Debug.Log(card.img);
       }
     }
     GUI.enabled = true;
     
     GUILayout.FlexibleSpace();
     GUILayout.EndHorizontal();   
   }
   GUILayout.FlexibleSpace();
   GUILayout.EndVertical();
 }
 
 function BuildDeck(){
     var card:Object; // this stores a reference to a card
     var id:int = 0;
     var num:int = 0;
        var otherNum:int = 2;
 
        var aColorParts:List.<String> = new List.<String>();
     aColorParts.Add("Green");
     aColorParts.Add("Yellow");
     aColorParts.Add("Red");
     aColorParts.Add("Orange");
     aColorParts.Add("Purple");
     aColorParts.Add("Pink");
     aColorParts.Add("LightBlue");
     aColorParts.Add("Blue");
    
     for(var j:int=0; j<2; j++)
     { 
            var theMissingPart:String = aColorParts[num];
    
            aColorParts.RemoveAt(num);
       
         card = new Card("color" + "Missing" + theMissingPart, id);
             aCards.Add(card);
          
         card = new Card("color" + theMissingPart, id);
         aCards.Add(card);
         
         if(otherNum % 2 == 1){
             num++;
         }
         otherNum++;
         id++;
    }
 }

When I run it, I get an error saying: "Object reference not set to an instance of an object" on this line:

 if(card.isMatched)

and an error saying: "Argument is out of range" on this line:

 aGrid[i,j] = aCards[someNum];

What is the problem?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SinisterRainbow · Feb 25, 2014 at 11:43 PM

You didn't add anything to the aCards list - right now it's empty, and is why argument is out of range. You used 'new' to make memory space for it, but then didn't add anything to it. i.e. you need to add the deck.. from 1...52, aCards.Add(ACARD).

Secondly, you will find Debug.Log("...") a much better friend than you realize. Otherwise posting to the answers every bug issue will take you 17 years to make a small card game. (=

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How does unity/javascript handle object references? 1 Answer

Dictionary.TryGetValue for js 1 Answer

how to use other objects from other scenes in a script? 3 Answers

"does not have a visible constructor that matches" while such a constructor exists, what am I missing? 1 Answer

Collision Detection with object already in detection area 1 Answer


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