- Home /
Object doesn't move if variable is used?
If I have this code:
if (bally>posy)
{
//above
transform.Translate(0,speed*Time.deltaTime,0);
print ("I should be moving up");
}
if (bally<posy)
{
transform.Translate(0,-speed*Time.deltaTime,0);
print ("I should be moving down");
}
Nothing happens, but if I have 1f, and -1f instead of "speed", then it works as it should. Anyone know why?
$$anonymous$$y guess 'speed' is not a private variable. If it is not a private variable then its initial value will be taken from the inspector rather than how you've initialized it at the top of the file.
speed is a public variable and the inspector has it at 1f as well
Answer by Huacanacha · Nov 01, 2013 at 07:15 PM
Add a Debug.Log to see what your speed value is. I'd say it is probably zero which will cause the translation to have no effect, or a very small number so the movement is barely noticeable.
It was at 0, however it is set here public float speed = 1f; and also at 1 in the inspector....
I changed it from public to private and it worked. Interesting....
If you change the default value of a public variable in a script after assigning that script to an object, I think the new default value isn't taken into account. $$anonymous$$aybe something like that happened. If you change the variable back to public now it will probably work... you could also just remove and add the script again if this happens in the future.