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 Nightvein · Jun 24, 2015 at 10:12 PM · updatenullreferenceexceptionprefab-instancecoroutine errorsproximity

Experiencing NullReferenceException only in build with Coroutine

Unity3d noob here. Was trying to get proximity detection (polling every frame/update) working when the FPS camera gets close enough to a gameobject called beacon. I'm storing all the beacons in an allBeacons array. The functionality is executing within Game mode in the editor, but it doesn't seem to be executing within the build? Initially I thought my beacon gameobject was somehow inactive while the game is running in the build, but from what I can understand it should be active once it is instantiated?

My beacon gameobject gets created from a prefab. I don't see the nullreferenceexception within the console when I run it in the editor. I read up on the coroutine actually being an object and being set on a change in state, but I'm not sure how to structure my code such that this is true. I currently have the detection set within the beacon class, but I am considering switching it to a different monobehavior considering the beacon class is part of a prefab. In any case, I don't know best practice.

 NullReferenceException: Object reference not set to an instance of an object
   at Beacon+<ProximityDetection>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
 UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
 UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
 Beacon:Update()

 void Update ()
 {        
     if (allBeacons.Length > 0) {
         for (int i = 0; i < allBeacons.Length; i++) {
             if(allBeacons[i] && allBeacons[i].activeInHierarchy){
                 StartCoroutine (ProximityDetection (allBeacons [i], 3.0f));
             }
         }
     }
 }

 IEnumerator ProximityDetection (GameObject b, float waitTime)
 {
     if (Vector3.Distance (fps.transform.position, b.transform.position) <= detectionRange && b.GetComponent<Light>().Equals(null)) {

         Light light = b.AddComponent<Light> ();
         light.GetComponent<Light> ().type = LightType.Point;
         light.shadows = LightShadows.Hard;
         light.GetComponent<Light> ().intensity = 2.0f;
         light.GetComponent<Light> ().color = new Color (UnityEngine.Random.value / 2.0f + 0.5f, UnityEngine.Random.value / 2.0f + 0.5f, UnityEngine.Random.value / 2.0f + 0.5f);

         yield return new WaitForSeconds (waitTime);
         StartCoroutine (ProximityDetection (b, 3.0f));
     } else {
         Destroy(b.GetComponent<Light>());            
     }
 }









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

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Jun 24, 2015 at 10:23 PM

The easiest way to avoid this is to check if the GameObject variable's value is null before trying to access it.

Either do it before starting the Coroutine:

 if(allBeacons [i] != null) {
     StartCoroutine (ProximityDetection (allBeacons [i], 3.0f));
 }

or inside to CoRoutine itself, since you CAN pass it a GameObject variable whose value is null, but once you actually try to access it, you will get the NullReference error.

 IEnumerator ProximityDetection (GameObject b, float waitTime)
  {
      if(b == null) {
           break;
      }
      if (Vector3.Distance (fps.transform.position, b.transform.position) <= detectionRange && b.GetComponent<Light>().Equals(null)) {
 //... and so on

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 Nightvein · Jun 25, 2015 at 06:46 PM 0
Share

so I actually tried that yesterday and printed a debug statement if it was null, but it wasn't so I think I'm probably using the yield return incorrectly. Note, like I said this works in game mode, but not build mode so I am looking for an explanation why.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accessing a prefab after instantiating results as null 1 Answer

NullReferenceException 1 Answer

How to start a Coroutine in another Script? 2 Answers

i am not able to control each instance of an object in update function.help. 0 Answers

NullReferenceException MaskableGraphic.UpdateCull() upon Loading Player Data. 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