Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AlphaGarg8447 · Apr 14, 2016 at 12:47 AM · nullreferenceexceptionvoxelnull reference exceptionvoxelsfindobjectsoftype

FindObjectsOfType giving out null

Alright, now this intrigues me. I've put the following line of code into a Start function on a CS script, and it gives out null even though I have several objects in scene with the component voxel.

 voxels = GameObject.FindObjectsOfType (typeof (voxel)) as GameObject[];

I guess its obvious I'm doing something wrong here, but the console doesn't debug anything but the NullReferenceExceptions a few lines ahead. Can anyone help? I need to get this to work since it's a key feature of my game and there aren't many free voxel engines out there that work with Unity 5.

PS.: I've tried Cubiquity but it doesn't have Unity 5 support sadly, and all other voxel terrain stuff I've seen out there usually cost more than 60 dollars which is currently out of my reach.

Comment
Add comment · Show 2
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 raybarrera · Apr 14, 2016 at 02:33 AM 0
Share

Are the gameobjects disabled when this line executes? That's one possible reason you might be encountering this issue.

avatar image Immanuel-Scholz raybarrera · Apr 14, 2016 at 10:10 AM 0
Share

Sorry, not want to nitpick here, just for reference if someone stumbles upon this:

  • FindObjectsOfType never returns null even if there are no objects found. No need to check for null. If nothing is found, an empty array is returned. In this case here, the null comes later by using the "as" operator.

  • GameObjects aren't disabled, they are deactivated. Only components can be disabled (and they would still be found by FindObjectsOfType). That sounds subtle but is a very different beast in Unity and easily confused. ;) (And yes - it does not help that ScriptableObject has OnDisable but can never be "disabled")

  • Just for completeness: Another criteria beside deactivated GameObjects, why objects might not be found is when their hideFlag is set to DontSave. Not sure why Unity decided this (annoys the heck out of me) but its like that since around 5.2.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Immanuel-Scholz · Apr 14, 2016 at 10:03 AM

FindObjectsOfType is inherited from type "Object" and returns an Object[] - not a GameObject[]. Even if all members of the array happen to be GameObject's, that is still an array of Object's.(1)

What you are doing with the "as GameObject[]" is to tell the compiler "if the value is an GameObject[] array, assign it. Otherwise assign null."

To get an voxel[] array directly, you can use the generic version of FindObjectsOfType:

 voxel[] allVoxels = GameObject.FindObjectsOfType<voxel>();

However, in your example you are asking for all GameObjects that have a "voxel" component, right? (In other words, "voxels" is of type GameObject[] not of type voxel[]).

So you could either change your code so that "voxels" is of type "voxel[]" and use the generic version above. Or you first fetch the array of voxels and then retrieve all gameObjects that belong to this array:

 voxel[] allVoxels = GameObject.FindObjectsOfType<voxel>();
 voxels = new GameObject[allVoxels.Length];
 for (int i = 0; i < voxels.Length; i++)
     voxels[i] = allVoxels[i].gameObject;

Or shorter using LINQ - if you include an "using System.Linq;" at the top:

 voxels = (from v in GameObject.FindObjectsOfType<voxel>() select v.gameObject).ToArray();

(But don't get too attached to LINQ. It allocates a lot of unnecessary memory which the Mono GC really hates..)


1) You can do more things with an Object[] array than you can do with a GameObject[] array. For example, you can assign a non-GameObject to the former and not to the latter. The other way around would work: Every GameObject[] array IS actually also an Object[] array. That is a difference to the generic List where List<GameObject> is NOT convertible into List<Object>. Its confusing, I know.

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

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

Null Reference Exception anytime scene is reloaded? 1 Answer

CAR ENTER EXIT 1 Answer

Why am I getting a null reference error? 1 Answer

Voxel terrain generation? 0 Answers

Unknown Cause of NullReferenceException? 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