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 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 hit, range))
         {
             if (hit.collider.transform.parent.tag == "Block")
             {
                 Destroy(hit.collider.transform.parent.gameObject);
                 
                 Vector3 hitPos = hit.collider.transform.parent.transform.position;
                 
                 int x = (int) hitPos.x;
                 int y = (int) hitPos.y;
                 int z = (int) hitPos.z;
                 
                 Destroy(levelData[x, y, z].gameObject); *** this is where the error is
                 dataScript.levelData = levelData;
             }
         }
     }

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

Comment
Add comment · 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
0

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

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