Reputation: 1
I'm fairly new to unity and C# and I'm trying to understand how to change the gravity scale of my character when I press the space bar. When I debug it, it says that Rigidbody2D.gravityScale
cannot be used as a method. Can someone explain why it brings this error and how to fix it?
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
private Rigidbody2D rb2d;
public float addGrav;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
//Understanding::: If key pressed down,
//Transform - method that manipulates position of object
//Translate moves transform in direction/distance of translation.
//transform.Translate(translation);
if (Input.GetKey (KeyCode.UpArrow))
{
transform.Translate (Vector2.up * moveSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow))
{
transform.Translate (Vector2.down * moveSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate (Vector2.left * moveSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
}
if(Input.GetKey (KeyCode.Space))
{
rb2d.gravityScale (addGrav);
}
}
}
Upvotes: 0
Views: 11654
Reputation: 56
It's because the compiler were reading it as a method since you used parentheses instead of equal sign. it should be like so.
rb2d.gravityScale = addGrav;
Upvotes: 2
Reputation: 606
rb2d.gravityScale is not a method, it's a field that you can assign value to.
use this
rb2d.gravityScale = addGrav; //to asign
//other example
rb2d.gravityScale += addGrav; //to add
rb2d.gravityScale -= addGrav; //to substract
Upvotes: 5