当前位置:网站首页>Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
2022-06-10 17:54:00 【God of Luo Xiu】
The conclusion is as follows :
Inherited Monobehaviour Class , It's not just constructors , But the whole construction process will be unity Spiritual execution many times
There is a problem in writing
public class TestScript : MonoBehaviour
{
TestObject1 testObject1 = new TestObject1();// By new many times , In fact, multiple objects are generated
TestObject2 testObject2;
public TestScript()
{
testObject2 = new TestObject2();// By new many times , In fact, multiple objects are generated
}
}
Write it correctly :
public class TestScript : MonoBehaviour
{
TestObject1 testObject1;// Just a statement
TestObject2 testObject2;// Just a statement
void Awake()
{
testObject1 = new TestObject1();// stay Awake In the initialization , It's just new once
testObject2 = new TestObject2();// Same only new once
}
}
summary :
stay Unity in , Inherited from Monobehaviour When instantiating a class of , All construction statements may be Unity Execute many times ( Including member declarations and constructors ), We cannot avoid this process . So don't do any initialization during the construction of these classes , Otherwise, it may lead to unpredictable errors .
Be careful : It's not that your programming habits have gone wrong , It is Unity The problem of mechanism . If not inherited Monobehaviour Please be bold to initialize in the member declaration or constructor , therefore ……
terms of settlement :
use Awake() or Start() Instead of constructors !
use Awake() or Start() Instead of constructors !
use Awake() or Start() Instead of constructors !
perhaps
Don't inherit Monobehaviour!
Don't inherit Monobehaviour!
Don't inherit Monobehaviour!
边栏推荐
猜你喜欢

matplotlib plt. Specific usage of text() - labeling points in a drawing

pands pd. Detailed parsing of dataframe() function

Talk about message oriented middleware (1) and AMQP

Abbexa 无细胞 DNA 试剂盒说明书

Penguin E-sports stops, and tiger teeth are hard to walk

Feign based remote call

Gateway服务网关
待办事项桌面插件,办公族的桌面好帮手

PCA principal component analysis tutorial (origin analysis & drawing, without R language)

华为matepad能成为你的笔记本电脑副屏?
随机推荐
Swin_Transformer源码解读
AI 加持实时互动|ZegoAvatar 面部表情随动技术解析
Leetcode String to integer(Atoi)
绘制混淆矩阵
Nacos registry
.NET 开源的免费午餐结束了?
丢失的遗传力--Missing heritability
Jouer avec la classe de fonctions de pytorch
LeetCode 321. Maximum number of splices***
Nacos configuration management
Canvas大火燃烧h5动画js特效
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
Mapbox GL development tutorial (11): loading line layers
Numpy - record
【抬杠C#】如何实现接口的base调用
Leetcode 321. Nombre maximum de raccords
Snabbdom virtual DOM (I)
The relationship between trees, forests and binary trees
com.netflix.client.ClientException: Load balancer does not have available server for client: userser
分享我做Dotnet9博客网站时积累的一些资料