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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kiour · Apr 24, 2012 at 01:59 PM · nullreferenceexceptionobject reference

NullReferenceException: Object reference not set to an instance of an object

Hello,

I have 16 gameobjects using the same script MotionDetector and each store in the private int "k" the number each object is clicked.

I also have this script

using UnityEngine; using System.Collections;

public class SodokuPuzzle : MonoBehaviour {

MotionDetector[] myObjects;

void Start() {

     myObjects = new MotionDetector[16]; // new array with 16 elements ( 0..15 )
 for (int i = 0; i <= 15; i++)
 {
     GameObject go = GameObject.Find("B"+ (i+1));
     if (go != null)
         { Debug.Log("got : "+i);
             myObjects[i] = go.GetComponent<MotionDetector>();
         }
     else
         Debug.Log("sommething if wrong");
     }
      

}

 // Update is called once per frame
 void Update () {
 

 
     if(myObjects[0].k+myObjects[1].k+myObjects[2].k+myObjects[3].k==10) //Check first rows sum is 10
     {Debug.Log("first row corect");
 
     }
     else
     {    
     Debug.Log("first row not corect");
     }
     
 
     
 if(myObjects[4].k + myObjects[5].k + myObjects[6].k +myObjects[7].k==10) //Check second rows sum is 10
     {Debug.Log("Second  row corect");
     g++;
     }
     else
     {    
     Debug.Log("second row not corect");
     }
     
     if(myObjects[8].k + myObjects[9].k + myObjects[10].k +myObjects[11].k==10) //Check third rows sum is 10
     {Debug.Log("Third  row corect");
     g++;
     }
     else
     {    
     Debug.Log("Third  row not corect");
     }
              
     
         
 }
 
 

}

It comes up with the error: NullReferenceException: Object reference not set to an instance of an object SodokuPuzzle.Update () (at Assets/SodokuPuzzle.cs:65)

If I delete 2 out of the 3 if statements (in the update function) it works fine. Why would it work for 1 IF statement and not 2 or 3? Please help.

Comment
Add comment · Show 5
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 fafase · Apr 24, 2012 at 02:05 PM 0
Share

Which one is line 65 exactly? Also, which if statement do you remove? Where is g declared?

avatar image numberkruncher · Apr 24, 2012 at 02:29 PM 0
Share

Not relevant to problem, but you have spelt the name of the game wrong: "Sudoku"

avatar image Kiour · Apr 24, 2012 at 02:40 PM 0
Share

line 65 is if(myObjects[4].k + myObjects[5].k + myObjects[6].k +myObjects[7].k==10)

and g is irrelavent to the problem

avatar image Kiour · Apr 24, 2012 at 04:16 PM 0
Share

THanks Julien I had duplicate name objects. I am just an idiot.

avatar image Julien-Lynge · Apr 24, 2012 at 04:18 PM 0
Share

Trust me, everyone is an idiot sometimes when program$$anonymous$$g. I'm going to add the comment as an aswer below - if you approve it it will mark the question as answered.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Julien-Lynge · Apr 24, 2012 at 04:19 PM

The error is telling you that one of your references (variables) in line 65 is null. The the following debugging to see what reference is null:

 if (myObjects[4] == null)
   Debug.Log("4 is null");
 if (myObjects[5] == null)
   Debug.Log("5 is null");
 if (myObjects[6] == null)
   Debug.Log("6 is null");
 if (myObjects[7] == null)
   Debug.Log("7 is null");

 if(myObjects[4].k + myObjects[5].k + myObjects[6].k +myObjects[7].k==10)
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

6 People are following this question.

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

Related Questions

Pause Script Suddenly Stops Working 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Object reference exists but also doesn't? 1 Answer

NullReferenceException: Object reference not set to an instance of an object ss.Update () (at Assets/ss.cs:21) 0 Answers

NullReferenceException with an Array 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