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 /
This question was closed Apr 26, 2018 at 03:48 PM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jormaje · Feb 04, 2018 at 04:52 PM · buildbuild-errornullreferenceexceptionnull reference exceptionafter

NullReferenceException after build

I'm having problems after build. In preview everything works fine but after build there are errors. After build I'm having the exception 'NullReferenceException: Object reference not set to an instance of an object' here:

 void hideTreeOfObjects(GameObject ob){
     for (int i = 0; i < ob.transform.childCount; i++) {
     
         //for some reason child = null in build, but child != null in preview
         GameObject child = ob.transform.GetChild(i).gameObject; /*line 229*/
         
         try{
             child.GetComponent<MeshRenderer> ().enabled = false;
         }catch(MissingComponentException){}
         try{
             if (child.tag != "lostdetector" && !VRmode){
                 child.GetComponent<Collider> ().enabled = false;
             }
         }catch(MissingComponentException){}
         try{
             child.GetComponent<LineRenderer> ().enabled = false;
         }catch(MissingComponentException){}
         hideTreeOfObjects (child);
     }
 }

This function disables some components for every GameObject in a hierarchy tree. It's called by this functions in the same script (UIManager.cs):

 //[...]
 
 public GameObject Indicators;
 
 //[...]
 
 void hideIndicators(){
     hideTreeOfObjects (Indicators);
 }
 
 public void OnClickRepository(){
     setMode (3);
     PathItem.hideAllPathPoints ();
     hideIndicators ();
     selectedObject = null;
     selectedObjectToCreate = null;
     refreshUI ();
 }
 
 void Start () {
     OnClickRepository ();
     refreshUI ();
     loadRepositoryUI ();0
 }
 

This is the GameObject Indicators. It has only the transform component:

alt text

The GameObject Indicators is assigned properly in the inspector of the GameObject which uses the UIManager component:

alt text

Why is this happening?

tree.png (28.2 kB)
manager.png (86.4 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by jormaje · Apr 26, 2018 at 02:46 PM

I've solved it by catching the NullReferenceException. The problem was that Unity throws diferent exceptions in preview and in build. In preview I was getting the MissingComponentException when I was using a function of an inexistent component, but not the NullReferenceException.

So at the end all component function calls like

 try{
     child.GetComponent<MeshRenderer> ().enabled = false;
 }catch(MissingComponentException){}

have turned into this

 try{
     try{
         child.GetComponent<MeshRenderer> ().enabled = false;
     }catch(NullReferenceException){}
 }catch(MissingComponentException){}

But there are better solutions to enable/disable components for all the descendants of a specified object, like this one

 public void Test2(GameObject ob)   
 {
         MeshRenderer[] list = ob.GetComponentsInChildren<MeshRenderer>();
         Debug.Log("Found: " + list.Length);
         foreach (MeshRenderer rend in list)
         {
             Debug.Log("Disabling: " + rend.name);
             rend.enabled = false;
          }
  }
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

Follow this Question

Answers Answers and Comments

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

Unity Build nullReference, but editor sees through it! 1 Answer

Bogus Null Reference in Build only,Bogus Phony NULL REFERENCE in BUILD ONLY (works in editor) 0 Answers

Input Working in Editor but not in Build 0 Answers

Null Reference Exception in iOS build for some users. Not in Editor and cannot duplicate 0 Answers

Can't create a build in Unity from ubuntu 0 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