- Home /
The question is answered, right answer was accepted
Can't change a float
I'm trying to make a powerup that changes the speed of the character but for some reason it is giving me errors. Here's the code:
public float speed = 1;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "PowerUp")https://answers.unity.com/questions/ask.html#
{
speed = speed + 0.3;
}
}
Try adding the line: Debug.Log("speed = " + speed);
after you change the speed variable and see what happens. That will tell you if the code is actually firing or not. If it isn't, try adding something like Debug.Log(col.gameObject.name);
before the if statement and see what happens then.
I'm assu$$anonymous$$g that that hyperlink isn't part of your actual code. That would definitely cause errors! XD
Answer by BenKrazier · Aug 11, 2018 at 10:12 PM
also I think you might need some "f"s after your float values or they read as "double" or "int"
speed = 1.0f;
speed += 0.3f;
Don't feel like that :P … I only saw it cause I do it all the time too :)
Answer by Strixie13 · Aug 11, 2018 at 09:58 PM
public float speed;
void Awake()
{
speed = 1;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "PowerUp")
{
speed += 0.3f;
}
}