当前位置:网站首页>[Solved] Unity Coroutine coroutine is not executed effectively
[Solved] Unity Coroutine coroutine is not executed effectively
2022-08-05 03:06:00 【cat running wild outside the mountain】
Development platform: Unity
Programming platform: Visual Studio 2020 or above
Language used: C#
Problem description
When calling an object that is frequently activated/deactivated, the coroutine (Coroutine) mounted by the object will only be executed when it enters Runtime mode for the first time.Subsequent deactivation and reactivation operations cause the content of the coroutine to not run correctly.
public RectMask2D Mask2D;public Vector4 Forward;public void Start(){StartCoroutine(DOLineMove);}public IEnumerator DOLineMove(){yield return Mask2D.padding -= Forward;StartCoroutinue(DOLineMove());}
Problem Analysis
The premise of running the coroutine is that the mounted GameObject object remains in the Active state.Note: The mounted GameObject object of the coroutine must remain in the Active state during execution or self-iteration).In the Unity life cycle, the initialization phase is Awake , OnEnable , and Start three cycle phases.Roughly as follows:
- The Awake stage is used to capture Component information on the script mount object.
- OnEnable phase is used for event listener registration behavior.
- The Start phase executes the entry procedure.

When Editor first enters Runtime mode, Initialization phase methods will be executed sequentially.So no matter at which stage the StartCoroutine(string methodName) is called, the IEnumerator type is effectively called.The problem is evident at the following levels:
- The internal call
StopCoroutine(string methodName)orStopAllCoroutine()is not standardized.
These two methods disable coroutine objects enabled within the script.One disables only specific objects, the other disables all objects within the script.Irregular use logic is easy to cause this problem. - The script object or the script object itself is automatically disabled and restarted by programs of unknown origin, causing the coroutine to run abnormally.
For example: Awake phase should execute coroutine methods correctly.But in the same Awake cycle stage, another script modified (·SetActive()orenable) state caused.In this case, the coroutine will not function properly.Combined with the execution cycle of the Unity program in Runtime mode, the next time it is enabled, it will be performed directly from theOnEnablestage.Of course, frequent disabling and re-enabling can also be problematic.(This must be a bug written by someone)
Solution: Troubleshoot Who is tampering with (or frequently) the state of the script?
Troubleshooting is often a troublesome thing, which is closely related to the amount of code of the project.It can be resolved quickly, or it can take a few hours, or even a week.Perhaps it was the framework that was not considered problematic at first, and the hidden problems were gradually exposed in the subsequent development.The best way is to follow the design of Loading into the scene on demand, rather than keeping it in the scene, activating, deactivating.If it is too late to save, then the next is the only possible effective method.
1) Use the Quick Find function to check one by one

Visual Studio provides editing for Quick Find .Usually this function is used to query keywords.Of course, it also plays a role in the investigation.Because it is related to disabling and enabling, search for the content associated in SetAtive() / enable in the code, and confirm the troubleshooting one by one.(Time-consuming is inevitable)
2) Use Browse Definitions to quickly locate references

Add anywhere in the program executablethis.gameObject.SetActive(false) or this.enable(true) , right-click the keyword SetActive or enable and select View AllQuote.Visual Studio will give all quoted statements that use this keyword.It is convenient and quick to check.Notice!
- Be sure to open all assemblies for this project when browsing for definitions.rather than individuals.
- In some cases, developers use self-encapsulated
GameObjectUtil.Disable(GameObject obj)and other methods.In this case, it is also necessary to check the object and location information on which the method is called and check one by one.
边栏推荐
- 627. Change of gender
- word分栏小记
- Why did they choose to fall in love with AI?
- leetcode - a subtree of another tree
- 龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
- VSCode Change Default Terminal how to modify the Default Terminal VSCode
- QT语言文件制作
- Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
- 1873. The special bonus calculation
- 22-07-31周总结
猜你喜欢

The Tanabata copywriting you want has been sorted out for you!

冒泡排序与快速排序

Flink 1.15.1 Cluster Construction (StandaloneSession)

A small tool to transfer files using QR code - QFileTrans 1.2.0.1

2022-08-04 第六小组 瞒春 学习笔记

链表的简单描述及代码的简单实现

View handler stepping record

倒计时 2 天|云原生 Meetup 广州站,等你来!

沃谈小知识 |“远程透传”那点事儿

21天学习挑战赛(2)图解设备树的使用
随机推荐
倒计时 2 天|云原生 Meetup 广州站,等你来!
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
QT:神奇QVarient
Details such as compiling pretreatment
Distributed systems revisited: there will never be a perfect consistency scheme...
(11) Metaclass
解决connect: The requested address is not valid in its context
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
The Tanabata copywriting you want has been sorted out for you!
Study Notes-----Left-biased Tree
数据增强Mixup原理与代码解读
开源协议说明LGPL
云原生(三十二) | Kubernetes篇之平台存储系统介绍
Data storage practice based on left-order traversal
rpc-remote procedure call demo
Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
Regular expression to match a certain string in the middle
引领数字医学高地,中山医院探索打造未来医院“新范式”