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 Meenlock · Jul 23, 2021 at 02:45 AM · 2d game2d-physicslogic

What is the infinite loop in the following code?

This is pretty bizarre, and to be frank I'm looking for a lark fix where somewhere my code is the issue. But I haven't found any.

I'm just a newbie developer that was messing around with a game making course. Which would probably mean that it's a mistake in the code somewhere. But I've taken it apart, and it's still not something that is resolvable with what I know. It might just mean there's a complication that others know about, but I sure don't know about it.

 protected override void OnCollide(Collider2D coll)
 {
     if (coll.name == "MC")
     {
         Debug.Log("collecting");
         GameManager.instance.SaveState();

         string sceneName = sceneNames[Random.Range(0, sceneNames.Length)];
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 }

Where OnCollide is just something that is called in "collidable"'s Update() for each BoxCollider2DComponent.OverlapCollider hit, passing the hit. Pretty standard stuff.

This game making course is not very complicated. As soon as it loads scene, everything should be destroyed, and assuming that 0,0 is not on top of the object calling OnCollide, nothing should result in an infinite loop. Obviously, that's the problem. Unity's editor player freezes and the scene cycles to itself, with infinite loops of SampleScene (not loaded), SampleScene and SampleScene, SampleScene (not loaded). As we all know, and I know now, loading the same scene is not the problem.

MC is not destroyed on load. I thought at first perhaps, that when it was loading, MC was preserved in that position and that the loadscenecollidable, for lack of a better term, was triggered. There was a fix to that that didn't seem to resolve that. When loadscenecollidable was set to not collide after the first collision, nothing changed. So OnCollide was not retriggered before loading. Maybe load scene reloaded the whole thing and OnCollide was triggered in the next scene, while MC was still there.

Looking for answers, it seemed to be that whenever there was an infinite loop, anywhere, that that was a likely candidate for this to happen. So a long trip back in my code to look for any loops to other loops, anywhere. Naturally there were no loops like that in so simple a project. So that's not something that is a problem. Although what I found next isolates the problem to this code in particular.

I wondered whether the code I SceneManager.sceneLoaded += was an infinite loop. I took it away. Infinite loop. I put it back. Infinite loop. It only causes one additional call a scene, but obviously I already had a SceneManager.sceneLoaded -=. Playing around with variations of that didn't help. I put in some Debug.Logs. What was in sceneLoaded was not even called. So then I moved the MC to a different position before LoadScene was called, which was what was in the sceneLoaded code, but that didn't break the loop, again. No real code in anything else sceneLoaded called and kind of disproves my first and second theories.

I took away LoadScene. No problems then. I took away the OnCollide, so it defaulted to something it derived from that stopped after triggering the first time. Did in fact stop after triggering the first time. I rewrote the code. I copied the code in from code that worked in case the ASCII was cursed. Everything worked as long as LoadScene was not in there.

Finally, I destroyed the object after LoadScene. I destroyed the object after moving MC. Guess what happened. MC was stuck in place, elsewhere, with the same infinite loop. I waited out in case all these triggers were from before all this happened. Still infinite.

One more thing. When MC is not DoNotDestroyOnLoad, and Awake does not destroy it if its a copy, it also works without an infinite loop, but no other object really works after a LoadScene either. Full of "missing scripts", but I'll get to that problem when I come to it. I've heard DoNotDestroyOnLoad doesn't remove scripts on load, but this post is long as is without reference to that. https://forum.unity.com/threads/scenemanager-loadscene-does-not-destroy-correctly-the-current-scene.378376/#post-2786778

Sure as hell SampleScene is in the build, else it won't load in the first place.

Your guess is better than mine.

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

0 Replies

· Add your reply
  • Sort: 

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

170 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to create soil or sand?? 1 Answer

HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers

RigidBody2d not detecting collision when dragged 1 Answer

Should a physics material be added to the collider or the rigidbody? 2 Answers

Push multiple objects away from explosion [2D] 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