Using variable name as moveSpeed doesn't effect enemy speed

When I try to use enemy speed variable as “moveSpeed”, until I change it in inspector, the enemy doesn’t move. Even if I set it as 1f by default. When I use other names like “Speed” the enemy moves on play. Why?

Hi Saulius,

Did you assign your script to the Inspector of the enemy game object before changing the value in your code? If so, that explains why nothing happened when you tried to change the value in your code.

When adding a component, the Inspector takes the default values from your code but controls the variables afterwards. If you change the values in your code, the values in your Inspector do not get updated again. That makes sense because you usually don’t want to lose the values that you set in the Inspector. (Imagine you have hundreds of different enemies with different values in the scene and they all have an Enemy component attached. You would lose your work if the values got overridden just because you changed something in your code.)

If you change the value in your Inspector and your enemy started to move, your code works correctly. If the enemy does not move even if you change the moveSpeed value in the Inspector, there is very likely an issue in your code.


See also:

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms