• 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
Question by Theacesofspades · Dec 07, 2014 at 04:45 PM · errorgameobjectdestroynull

Why am i getting a null reference?

My Script is:

 void DestroyBlock ()
     {
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out $$anonymous$$t, range))
         {
             if ($$anonymous$$t.collider.transform.parent.tag == "Block")
             {
                 Destroy($$anonymous$$t.collider.transform.parent.gameObject);
                 
                 Vector3 $$anonymous$$tPos = $$anonymous$$t.collider.transform.parent.transform.position;
                 
                 int x = (int) $$anonymous$$tPos.x;
                 int y = (int) $$anonymous$$tPos.y;
                 int z = (int) $$anonymous$$tPos.z;
                 
                 Destroy(levelData[x, y, z].gameObject); *** t$$anonymous$$s is where the error is
                 dataScript.levelData = levelData;
             }
         }
     }

null reference exception: object reference not set to an instance of an object

Comment

People who like this

0 Show 7
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 thepenguinmaster · Dec 07, 2014 at 04:57 PM 0
Share

No one will be able to help you pinpoint the problem here. The problem is not always where you are getting the error, but sometimes where the object is created in this case.

In the eample you provided I do not see where levelData is declared and set. Can you post the code where you populate the data in levelData?

avatar image cariaga · Dec 07, 2014 at 06:28 PM 0
Share

you point out the error but what does levelData look like? you should include that too.

avatar image static_cast · Dec 08, 2014 at 01:18 AM 0
Share

Does levelData exist, is it filled, and does it have a set size?

avatar image NoseKills · Dec 08, 2014 at 09:49 PM 0
Share

Can you tell us the line that causes the error please?

Double clicking on the error in Unity Console window should take you to the exact line in code where the error happens. From there it should be pretty easy to deduct the reference that's causing the error.

avatar image NickvanDokkum · Dec 09, 2014 at 12:49 PM 1
Share

have you tried doing prints just before the error? like:

print(levelData[x, y, z]); print(levelData[x, y, z].gameObject);

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by thepenguinmaster · Dec 09, 2014 at 02:27 PM

levelData[x, y, z] is throwing an error because you never declared the variable. Also you need to set data into the array. It is likely that where you are populating the data, one of the objects is null. Given the code you posted, there is no way for us to provide further help.

The code you posted is where you see the error, but not where the error is caused. You will need to post the code where the array is declared, and the values are set into the array, then we may be able to help.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

missing gameobject ≠ Null? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer

Object reference not set to an instance of an object (with a RaycastHit) 3 Answers

Can't destroy objects 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