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 /
avatar image
1
Question by TSI25 · Sep 14, 2016 at 06:34 PM · gameobjectassetbundlemissingreferenceexception

Assigned variable returns null?

I have what I believe to be a very straightforward script that effectively just enabled gameobjects in the scene when certain actions are completed. I'm running into an issue where when i try to enable them they will occasionally evaluate to null and I will hit a missing reference exception as if the object has been deleted - but if i look at the object in the inspector I can see that it is definitely assigned (its a public variable.)

stuff I've tried:

  • I made sure there isn't another instance of this class in the scene that hasn't been initialized properly. there is only one instance, and I can see that its gameobjects are assigned as anticipated.

  • Reassigning the game objects at runtime. I figured maybe something was weird about how they were assigned, so I tried assigning them manually at run time in the inspector but no joy, it still gives me a missing reference exception

  • Manually enabling it before trying to reference it. I figured maybe something weird could be happening where its having trouble retrieving a disabled gameobject but that still gave me a missing reference exception.

I'll post the code thats enabling it below as well as what it looks like in the inspector but its really about as straightforward as it gets, maybe I'm missing something very simple though.

Things that might make this weird :

  • This class and the objects its enabling are loaded into the scene via asset bundle. unity has historically had some weirdnesses associated with loading asset bundles so maybe theres a bug to do with that in play?

  • the function gets called as the result of an event being fired elsewhere. I'm not sure why this would matter since it doesnt seem like its a problem with the wrong function being called/the wrong parameter being passed but ive had troubled with events and listeners before.

any help would be appreciated.

*EDIT here is the stack trace

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. HFN.Ecomuve.TrailerMap.OnPlaceTracerPin (HFN.Ecomuve.TracerModule tracerModule) (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TrailerMap.cs:32) HFN.Ecomuve.TracerTool.Update () (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TracerTool.cs:149)

alt text

 using UnityEngine;
 using System.Collections;
 using HFN.Common;
 
 namespace HFN.Ecomuve
 {
     public class TrailerMap : MonoBehaviour 
     {
         public GameObject farmTracerPin;
         public GameObject golfTracerPin;
         public GameObject neighborhood1TracerPin;
         public GameObject neighborhood2TracerPin;
         public GameObject toiletTracerPin;
 
 
         public void PlaceTracerPin(TracerModule tracerModule)
         {
             switch(tracerModule.tracerTypes)
             {
             case TracerId.Farm :
                 Debug.Log(farmTracerPin);
                 farmTracerPin.SetActive(true);
                 break;
 
             case TracerId.GolfCourse :
                 Debug.Log(golfTracerPin);
                 golfTracerPin.SetActive(true);
                 break;
 
             case TracerId.Neighborhood1 :
                 Debug.Log(neighborhood1TracerPin);
                 neighborhood1TracerPin.SetActive(true);
                 break;
 
             case TracerId.Neighborhood2 :
                 Debug.Log(neighborhood2TracerPin);
                 neighborhood2TracerPin.SetActive(true);
                 break;
 
             case TracerId.Toilet :
                 Debug.Log(toiletTracerPin);
                 toiletTracerPin.SetActive(true);
                 break;
             }
         }
 
 
         public void PlaceTracerPin(Tracer tracer)
         {
             switch(tracer.tracerId)
             {
             case TracerId.Farm :
                 farmTracerPin.SetActive(true);
                 break;
 
             case TracerId.GolfCourse :
                 golfTracerPin.SetActive(true);
                 break;
 
             case TracerId.Neighborhood1 :
                 neighborhood1TracerPin.SetActive(true);
                 break;
 
             case TracerId.Neighborhood2 :
                 neighborhood2TracerPin.SetActive(true);
                 break;
 
             case TracerId.Toilet :
                 toiletTracerPin.SetActive(true);
                 break;
             }
         }
 
 
         private void Awake()
         {
             TracerTool.onSaveTracer += PlaceTracerPin;
         }
     }
 }
inspector.jpeg (98.8 kB)
Comment
Add comment · Show 3
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 Bunny83 · Sep 14, 2016 at 06:50 PM 0
Share

First of all +1 for the long an detailed description. $$anonymous$$ost don't take the time to actually explain their situation ^^.

However there's still some things missing:

  • First, those gameobjects you reference, where are they? They don't seem to exist in the scene so they must be assets?!?

  • If they are assets you can't set them active you would need to instantiate them into the scene.

  • In your code you have two "PlaceTracerPin" methods (C%P error?). You said you load those gameobjects manually. Can we see that code?

  • $$anonymous$$issingComponentException seems a bit strange in this case. You don't deal with any components here.

  • Since you seem to receive errors, could you copy and past the exact error including the stacktrace? If you don't want to post it, have you analysed the stacktrace if there is any?

avatar image TSI25 Bunny83 · Sep 14, 2016 at 07:03 PM 0
Share

Excellent questions.

Those objects are totally in the scene, the inspector screenshot that shows the hierarchy was intended to show that there is only one trailer map object. Here is another inspector screenshot that shows where those objects are in the actual hierarchy. alt text

I'm not sure what you mean with loading manually. the asset bundle gets loaded in when the the scene loads, i just tried manually dragging those objects into the inspector. I have tried disambiguating the PlaceTracerPin call such that I had two separate function OnPlaceTracerPin and PlaceTracerPin just in case there was some issue with the event listener but that didn't seem to make any difference.

Here is that stack trace.

$$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. HFN.Ecomuve.Trailer$$anonymous$$ap.OnPlaceTracerPin (HFN.Ecomuve.Tracer$$anonymous$$odule tracer$$anonymous$$odule) (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/Trailer$$anonymous$$ap.cs:32) HFN.Ecomuve.TracerTool.Update () (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TracerTool.cs:149)

inspector.jpeg (95.8 kB)
avatar image SarfaraazAlladin · Sep 14, 2016 at 07:37 PM 0
Share

Hey,

It might help to see your TracerTool code as well. Off the top of my head, the only thing I can think of is that the variable that onSaveTracer is sending is where the actual issue is. are you sure that Tracer$$anonymous$$odule tracer$$anonymous$$odule & Tracer tracer are actually being passed in correctly?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TSI25 · Sep 14, 2016 at 08:16 PM

Well I came up with a work around but im still not sure why i was getting the behavior i was getting. If someone finds out let me know. For my workaround I basically just wrote a separate class for the objects getting enabled to track their type and their enable-able object. Then when retrieving it i check if its null and if so I iterate through the trailer map class' children looking for it (and assign it).

Seems to work, here is the new code for that in case others find it useful.

 using UnityEngine;
 using System.Collections;
 using HFN.Common;
 
 namespace HFN.Ecomuve
 {
     public class TrailerMap : MonoBehaviour 
     {
         public TracerPin farmTracerPin;
         public TracerPin golfTracerPin;
         public TracerPin neighborhood1TracerPin;
         public TracerPin neighborhood2TracerPin;
         public TracerPin toiletTracerPin;
 
         public TracerPin GetPin(TracerId id)
         {
             TracerPin pin = null;
 
             switch (id)
             {
                 case TracerId.Farm:
                     if (farmTracerPin == null) farmTracerPin = FindPin(id);
                     pin = farmTracerPin;
                     break;
 
                 case TracerId.GolfCourse:
                     if (golfTracerPin == null) golfTracerPin = FindPin(id);
                     pin = golfTracerPin;
                     break;
 
                 case TracerId.Neighborhood1:
                     if (neighborhood1TracerPin == null) neighborhood1TracerPin = FindPin(id);
                     pin = neighborhood1TracerPin;
                     break;
 
                 case TracerId.Neighborhood2:
                     if (neighborhood2TracerPin == null) neighborhood2TracerPin = FindPin(id);
                     pin = neighborhood2TracerPin;
                     break;
 
                 case TracerId.Toilet:
                     if (toiletTracerPin == null) toiletTracerPin = FindPin(id);
                     pin = toiletTracerPin;
                     break;
             }
 
             return pin;
         }
 
 
         public TracerPin FindPin(TracerId id)
         {
             TracerPin pin = null;
             TracerPin[] pins = GetComponentsInChildren<TracerPin>();
 
             foreach (TracerPin p in pins)
             {
                 if (pin.tracerId == id) pin = p;
             }
 
             return pin;
         }
 
 
         public void OnPlaceTracerPin(TracerModule tracerModule)
         {
             TracerPin tracerPin = GetPin(tracerModule.tracerTypes);
             Debug.Log(tracerPin);
             tracerPin.Show();
         }
 
 
         public void PlaceTracerPin(Tracer tracer)
         {
             TracerPin tracerPin = GetPin(tracer.tracerId);
             Debug.Log(tracerPin);
             tracerPin.Show();
         }
 
 
         private void Awake()
         {
             TracerTool.onSaveTracer += OnPlaceTracerPin;
         }
     }
 }
Comment
Add comment · Show 1 · 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
avatar image juicyz · Sep 15, 2016 at 06:44 PM 0
Share

Can't give you an exact answer either as it seems everything would need to be debugged as there is a lot of objects and code going on. It seems Bunny hits the answer somewhere in those points. It's bound to be one of those. I just wanted to comment that error checking is good :D

Should always check if an object is null before using it. (Good program$$anonymous$$g that helps you debug)

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

74 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

Related Questions

GameObject reference lost between functions 0 Answers

DontDestroyOnLoad error 2 Answers

How to use loading text 0 Answers

Why I get Unity Ads Missing Reference error? 1 Answer

Keep GameObject to variable linkage on LoadLevel 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