Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable not updating in Unity [closed]

So in visual studio line goes

public float 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.

like image 509
Domagoj Buljan Avatar asked Apr 11 '26 03:04

Domagoj Buljan


1 Answers

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.

However, if you look at the component, in the inspector, you will see a little gear on the right. Click on it and click on Reset. The value you have set in code will be set in the inspector. But be careful, all your class'members will be reset too.

Reset

You can override the MonoBehaviour's Reset function if you want.


An other workaround would be to set your variable private, let Unity reserialize your component and put the member back to public. (But it may break your code since other scripts may need to reference your class member)

like image 186
Hellium Avatar answered Apr 13 '26 17:04

Hellium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!