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
0
Question by FerdieQO · Apr 20, 2017 at 08:58 AM · c#freezereflectionforeach

Unity freezes after invoking method in loop of the same method (C#)

First of all, my apologies for the vaguely formulated title. After hours of trying to understand and solve this problems (the last one in vain), I'm not capable of formulating the problem in one small sentence.

Context

I've built a system in which certain methods (we call them 'Actions') can be visually scripted when certain conditions are met, e.g. MoveToPosition. Since actions should be able to have sequential actions (triggered after the end of an action), the Action instances have a property NextActions, which is a List of IDs. Every time an action has ended, the event OnActionEnd is raised, which leads to the following method.

 public void OnActionEnd()
 {
     foreach(string actionID in nextActions)
     {
         ActionManager.InvokeAction(actionID);
     }
 }

 public class ScheduleAction
     {
         // ID of the Action
         public string actionID;
         
         // GameObject that holds the Component in which the
         // method that needs to be invoked resides
         public GameObject targetObject;
         
         // The name of the method that needs to be invoked
         public string method;
         
         // The list of parameters that will be passed to the
         // method that will be invoked
         public List<object> parameters;
         
         // List of Action IDs that need to be triggered after
         // the method of this action reached his end
         public List<string> nextActionIDs;
         
         // Flag indicating if the Action should be triggered or not
         // (could be false for debugging purposes)
         public bool activated;
 
         public void InvokeAction()
         {
             if (activated && !string.IsNullOrEmpty(method))
             {
                 string componentName = method.Substring(0, method.IndexOf("/"));
                 string methodName = method.Substring((method.IndexOf("/") + 1));
                 if (!string.IsNullOrEmpty(componentName) && !string.IsNullOrEmpty(methodName))
                 {
                     targetObject.InvokeInComponent(componentName, methodName, parameters.ToArray());
                 }
             }
         }
     }


Problem

Now for the problem... Unity freezes and ultimately crashes during this method. Why? Because when OnActionEnd is triggered after an action, it begins the foreach loop with invoking the first action in the list NextActions. Since that NextAction could be just one line of code (e.g. SetColor), it will immediately trigger OnActionEnd, causing the loop to.. loop. Forever, because the first call to OnActionEnd has never finished and is at that time still at the ActionManager.InvokeAction(actionID); line.

Unfortunately, I don't know the details of what's exactly going wrong nor have I got a solution for this. I've got a feeling I'm totally overlooking this, but since I'm the only programmer on the team I don't have someone to brainstorm with.

Tried solutions

  • I've tried a multi-threaded approach, which seemed to work at first, until I started invoking methods involving Unity components.. (you can't invoke UnityEngine methods outside the main thread).

  • I've tried using delegates with the event system but since the ScheduleAction script needs to be serializable (thus can't inherit from MonoBehaviour), I can't use the OnEnable() and OnDisable() methods to subscribe the InvokeAction() to events.

  • I've tried using a regular for-loop, to no avail.

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 ExtinctSpecie · Apr 20, 2017 at 09:05 AM 0
Share

did you try using a simple for ins$$anonymous$$d of for each? unity crashes when there is an infinite loop that happened to me as well and i was mad because there is no autosave :P

avatar image FerdieQO ExtinctSpecie · Apr 20, 2017 at 09:10 AM 0
Share

@ExtinctSpecie, thank you for your suggestion. As a matter of fact, I've actually tried that as well, to no avail. I've included it in the tried solutions section.

avatar image NoseKills · Apr 20, 2017 at 03:45 PM 1
Share

To me it sounds like if you have 2 actions that both have eachother as "follow up actions" and you execute either one of them, it will already create the kind of endless recursion you are dealing with.

Even if there were actions without follow ups, all it takes is 2 actions that end up executing eachother and you're stuck.

I guess first you at least have figure out if this is the problem. If it is, that needs to be solved on paper, design-wise before writing more code.

If you have actions ColorizeToGreen and ColorizeToRed and they both lead into executing eachother, what should the result be? A red object, a green object or an object that changes color every x seconds?

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

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

Get Component or Check if Null SoftLocks Game (C#) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to spawn an animal at each transform of a gameobject with a tag position in another scene? 0 Answers

Application.LoadLevel freeze at first load 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