Power up button
hello,i've been trying to create a power up button that adds more speed to the player's speed,but i can't figure out how to acces and modify the speed float from one script to the one that activates when the button is pressed,this is the code that i have untill now
public class AbilitySpeedUsed : MonoBehaviour { // this is used to make a reference to ability speed lock script,and the unlock script
public AbilityLock locked;
public AbilityUnlock unlocked;
//multiplier for how many times the player speed
public float multiplier =1.4f;
// Start is called before the first frame update
public void AbilityUsedSpeed()
{
//sets the button to not be interactible again
locked.enabled = true;
Debug.Log("powerspeed");
unlocked.enabled =false;
pickup();
}
//pickup function is where the player clicks the button and gets the boost in speed
void pickup()
{
Debug.Log("YouUsedthespeedbutton");
//ads speed to the already constantly moving player
(gameobject().Playermovement().forwardforce * multiplier);
}
}
and the script to where the float is
public class PlayerMovement : MonoBehaviour
{// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Simply, set speed to speed + powerUp advantage. Set powerUp to 0. Then, when the player clicks on the powerup button, increase the powerUp variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add a power up that adds time to timer script? 0 Answers
How does this variable equal two different values 1 Answer
Input.GetMouseButtonDown Code Running When Canvas Buttons Pressed 0 Answers
How can I select and deselect buttons when a new button is clicked? 0 Answers
'GameObject' does not contain a definition for 'Find' [Assembly-CSharp]csharp(CS0117) 0 Answers