Can I change Surface Effector 2D Speed in script?

So I was thinking about a Snow Boarder game from Unity 2D course and I would like to add a few coins like in Delivery Driver but when picked up I would like Snow Boarder to get a speed boost.

My question here is - Can I change Surface Effector 2D Speed in script?

For example if Character lands on Surface Effector 2D with boost picked up that would result in Surface Effector 2D Speed to increase for few seconds.

Hi,

Theoretically, it should be possible to access the Surface Effector 2D component via GetComponent. Then look the Surface Effector 2D class up in the API. If you know what you want to access in the Inspector, you can usually find the C# equivalent in the API. In both game projects, we accessed members via code, so you definitely have the knowledge and skills to make your idea work. :slight_smile:

If you want to set the speed back to the initial speed value, look up coroutines in the manual and the API.

Did this help?


See also:

Hey Nina,

Thank you for assistance. I have tried a few attempts but none seem to work. Apparently the code works but only when IF statement is not included in “Pickup1” script, as soon as I add IF statement nothing happens, however without IF statement code works but speed is changed every time my character touches the ground and I want it to happen only after the coin is picked up.

Script named “PickUp” is attached to the character and script named “PickUp1” is attached to the Level Sprite Shape.
Pickup

1 Like

Good job so far. Since the code works without the if-statement, you know that overriding the speed works as expected.

The script execution order in Unity is arbitrary. If the collision method of PickUp1 gets invoked before the collision method of PickUp, the speed won’t get overridden. The solution would be to predefine the execution order of these two scripts in the MonoManager.

If that did not fix it, add Debug.Logs to your collision method(s) to figure out what part of the condition is getting evaluated to false. Then try to figure out why it is getting evaluated to false.


See also:

I did it. It finally works with IF statement, however it works every time my character touches the ground, but I want it only when coin is picked up, I have no idea what else to try there anymore. Should I move the topic to the Unity forum in Help Needed section about scripting?

What you describe sounds as if your solution is almost complete. If your code works all the time, not just in the cases you want, that’s perfect because all you need is a restriction.

The computer is not intelligent. It cannot read our minds, it does not know anything about our ideas. If something happens that is not supposed to happen, we usually add an if-statement to our code which prevents that “something” from happening.

And if we want something to happen only once, the default solution is usually a flag (= bool variable). We change the value of the flag where the “something” happened the first time in our code, and our if-condition checks the value of the bool.

If necessary, make a sketch of your idea and define the “rules” of your game exactly. Then the implementation will become much easier. In programming, we rarely write a complete solution from scratch. We usually break the problem down into subproblems and solve the subproblems. Not getting the perfect result at the first onset is fine as long as you are making a step forward to your goal.

The solution was actually in the next lecture. Thank you Nina for your assistance. Topic can be closed now.

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

Privacy & Terms