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 Rambovalle · Aug 17, 2021 at 02:39 PM · foreachloops

after foreach . my Update() doesn't continue

I have foreach in Update() and after the foreach loop it moves to run other scripts updates and when it comes back it starts the update again.

top of my Update():

 void Update()
 {
     GameObject[] nearby = GetNearbyObjects(SeeDistance);
     GameObject nearestHouse;
     GameObject nearestFood;

     List<GameObject> houses = new List<GameObject>();
     List<GameObject> foods = new List<GameObject>();

     foreach (var obj in nearby)
     {
         if (obj.CompareTag("House")) { houses.Add(obj); }
         else if (obj.CompareTag("Eatable")) { foods.Add(obj); }
     }

     nearestHouse = getNearest(houses);
     nearestFood = getNearest(foods);

I used debugging mode to see what it runs and when and it didn't run anything more in that script's Update after the foreach loop before it starts over.

And unity gives me an error: NullReferenceException: Object reference not set to an instance of an object HumanBrain.Update () (at Assets/HumanBrain.cs:33)

the line that error is referring is: if (obj.CompareTag("House")) { houses.Add(obj); } inside the Foreach loop

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
1
Best Answer

Answer by Bunny83 · Aug 17, 2021 at 03:40 PM

Well, your issue is pretty obvious. Your "nearby" list or array contains elements that are either null, or are dead objects. When an exception happens the code can not continue since this is an exceptional state that the code can not recover from. So whenever an exception happens, the next upper exception handler will be called to sort things out. To handle exceptions manually you would use a try-catch block. Whenever an exception is thrown inside the try block, the execution is terminated and continued in the catch block. If there is no try-catch block, the exception will bubble up all the way to Unity. Unity will actually catch the exception at the point where it called your Update method. This is the point where Unity will generate the log message and continue with business on the next object.


This is the most fundamental knowledge how computers work and doesn't have much to do with Unity. In a "normal" application when an exception is not catched at all, the exception will bubble up to the operating system which will terminate your whole application because the OS has no idea how or where it should continue your application. If such an error happens in the OS / kernel code the whole OS can not continue. In this case you get a kernel panic, (aka blue screen on windows). Luckily the OS kernel nowadays is seperated and protected enough from normal applications that this is almost impossible.


All that said, in order to "fix" your problem you want to either make sure your "nearby" collection does not contain invalid elements, or if you can have null values or dead objects in that collection by design, you should check if the value is null. The easiest solution is a guard clause like this:

 foreach (var obj in nearby)
 {
     if (obj == null)
         continue;
     if (obj.CompareTag("House")) // [ .... ]

So if the current element we are processing is null, we simply skip the rest of the for loop body and just "continue" with the next element.

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

124 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

Related Questions

Turn off script in multiple objects 1 Answer

Why aren't the right amount of blocks instantiating? 0 Answers

Foreach loop not going through all the children 1 Answer

How do i use foreach/for loops properly? 1 Answer

C# Can't Instantiate object and can't change Instatiate position of object 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