0

I am trying to convert some UnityScript code to D# and I am getting the following error:

Expression denotes a method group, where a variable, value or type was expected on the Getcomponent

void  Update ()
{
    float xVel = GetComponent().Rigidbody2D().velocity.x;
    if( xVel < 18 && xVel > -18 && xVel !=0){
        if(xVel > 0){
            GetComponent.Rigidbody2D().velocity.x=20;   

        }else{
            GetComponent.Rigidbody2D().velocity.x = -20;

        }
    }
}
3

2 Answers 2

5

Your issue is with: GetComponent().Rigidbody2D() as that is not how you use GetComponent, the error you are seeing is probably because GetComponent requires a parameter or a specified type. The JS and C# GetComponent work a little different. You probably mean to do:

void  Update ()
{
    float xVel = GetComponent<Rigidbody2D>().velocity.x;
    if( xVel < 18 && xVel > -18 && xVel !=0){
        if(xVel > 0){
            GetComponent<Rigidbody2D>().velocity.x = 20;   

        }else{
            GetComponent<Rigidbody2D>().velocity.x = -20;

        }
    }
}

Also in C#, I don't think you can modify the velocity directly, due to the property wrappers around it. Instead you have to manually update the velocity to a new Vector2. If you only want to set the x value, pass in the existing y value.

I would write it something like this:

private Rigidbody2D _rigidBody;

void Start()
{
    _rigidBody = GetComponent<Rigidbody2D>();
}

void  Update ()
{
    float xVel = _rigidBody.velocity.x;
    if( xVel < 18 && xVel > -18 && xVel !=0){
        if(xVel > 0){
            _rigidBody.velocity = new Vector2(20, _rigidBody.velocity.y);   

        }else{
            _rigidBody.velocity = new Vector2(-20, _rigidBody.velocity.y);   
        }
    }
}

Though I'd change that magic 18 in to a variable too but I can't make a guess here as to what it represents!

Sign up to request clarification or add additional context in comments.

6 Comments

It still gives me this error : Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity'. Consider storing the value in a temporary variable
I think velocity cannot be modified manually. You will need to do: _rigidBody.velocity = new Vector2(newXHere, _rigidBody.velocity.y);
sorry to bother you but you have been really helpful i am getting an error about this code _rigidBody.velocity = new Vector2(y/2, _rigidBody.velocity.x) + colInfo.collider.GetComponent<Rigidbody2D>().velocity.y/3; its saying The best overloaded method match for UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments and Argument #1' cannot convert object' expression to type float'
That's because you're trying to add the left hand side, which is a Vector2 to just y/3 which is a float. Totally assuming here that you mean: _rigidBody.velocity = new Vector2(y/2, _rigidBody.velocity.x) + new Vector2(0, colInfo.collider.GetComponent<Rigidbody2D>().velocity.y/3); I may have got the x/y the wrong way round but that's the best guess I can make.
and again :( : 1.The name y' does not exist in the current context , 2.The best overloaded method match for 3.UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments Argument #1' cannot convert object' expression to type `float'
|
2
float xVel = GetComponent<Rigidbody2D>().velocity.x;

GetComponent is used like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.