当前位置:网站首页>[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.
边栏推荐
- 剑指Offer--找出数组中重复的数字(三种解法)
- Why did they choose to fall in love with AI?
- How Jin Cang database correctness verification platform installation file
- 21天学习挑战赛(2)图解设备树的使用
- QT MV\MVC结构
- 冒泡排序与快速排序
- Data storage practice based on left-order traversal
- 1873. 计算特殊奖金
- Review 51 MCU
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
猜你喜欢
随机推荐
private package
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
sql server 安装提示用户名不存在
1667. 修复表中的名字
OpenGL 工作原理
The design idea of DMicro, the Go microservice development framework
1873. 计算特殊奖金
Use @Mapper to query the partition status of oracle and report an error
undo problem
汉字转拼音
The 20th day of the special assault version of the sword offer
Hash table lookup (hash table)
语法基础(变量、输入输出、表达式与顺序语句)
leetcode - symmetric binary tree
Ant Sword Advanced Module Development
Study Notes-----Left-biased Tree
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
QT language file production
Principle and Technology of Virtual Memory








