变量未在 Unity 中更新 [英] Variable not updating in Unity
问题描述
所以在 Visual Studio 中是这样的
So in visual studio line goes
public float enemySpeed = 0.2f;
但是在 Unity 中,当我将其更改为 enemySpeed = 1
时,即使我停止游戏"并重新启动 Unity,它也会保持原样,即使它的 enemySpeed = 0.2f
> 在脚本中.
but in Unity when I change it to lets say enemySpeed = 1
it stays like that even when I stop "Game" and restart Unity even tho its enemySpeed = 0.2f
in script.
推荐答案
更改公共成员声明和初始化旁边的值不会在检查器中更改.Unity 序列化您的公共变量后,该值将一直保留,直到您在检查器中更改它.
Changing the value next to the declaration and initialization of the public member won't change in the inspector. Once Unity serialize your public variable, the value is kept until you change it in the inspector.
要添加 @Hellium 的答案,您可以将 [System.NonSerialize]
属性添加到您的变量中,这样它就不会被序列化.
To add on @Hellium 's answer, you could add the [System.NonSerialize]
attribute to your variable so it is not serialized.
有关该主题的更多信息:[NonSerialized] 与 [HideInInspector]] 问题
More on the topic: [NonSerialized] vs [HideInInspector] question
示例:
[System.NonSerialized]
public float enemySpeed = 1;
这样做的一个副作用是您将无法再通过检查器更改变量,只能通过脚本.
One side effect of this is that you won't be able to change the variable from the inspector anymore, only by script.
这篇关于变量未在 Unity 中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!