Error when trying to access GetComponent<Rigidbody>().velocity

I am writing this script:

    void OnTriggerEnter(Collider col)
    {
        Rigidbody targetRigidbody = col.GetComponent<Rigidbody>();
        if (targetRigidbody)
        {
            Speedboost(targetRigidbody, 3);
        }
    }
    public void Speedboost(Rigidbody m_Rigidbody, float SpeedAmount)
    {
        Vector3 OriginalSpeed = m_Rigidbody.GetComponent<Rigidbody>().velocity;
        //StartCoroutine(PowerUpTime());
        //m_Rigidbody.velocity = OGspeed;
    }

for some reason it doesn’t recognize “.velocity” and this error message is shown:


“‘Rigidbody’ does not contain a definition for ‘velocity’”…
and no matter how I try to access the velocity of the rigidbody it won’t recognize that command, even though it does exist in the Unity official scripting api.
What am I doing wrong?

Firstly, why are you using GetComponent to search for a Rigidbody when you clearly already have a Rigidbody, ‘m_Rigidbody’.

Also:

Otherwise we can barely read your code.

Use code tags!

Looks like you’ve made your own script named “Rigidbody”. That’ll cause the compiler to think that “Rigidbody” is your script rather than the built-in Rigidbody.

2 Likes

because in a different thread I read that you now have to use Get Component in order to access the velocity, I’ll read about the code tags, thanks.

Strange, I don’t remember having a class named Rigidbody, I should mention that this is “extending” the tank game tutorial, this is why I could be a little clueless.
Oh, I just noticed I could add UnityEngine.Rigidbody in order to call the built-in Rigidbody,this part compiles now, thanks!

No, you have to use GetComponent in order to get a specific component, since the properties are deprecated nowadays (or have already been removed in recent versions - not quite sure). Once you have an instance of the component type, you can simply access the exposed members.

//Given that 'rb' is already of type Rigidbody:
... = rb.velocity;
// and
... = rb.GetComponent<Rigidbody>().velocity;
// are identical except for a little more overhead in the latter version

That is, you don’t need to use GetComponent again as your parameter for the Speedbost method is already of type Rigidbody.

There are several situations in which you’ll be forced to use the fully-qualified name.
The one you’ve encountered pretty much hints to what @Baste has pointed out. I’d try to resolve the issue.

Thanks for the information! yeah, @Baste was right and I resolved the issue by using GetComponent<UnityEngine.Rigidbody>().velocity instead.
3166029--241069--moreerrors.png
Now plenty of other errors occurred that weren’t here before, but I doubt that has to do with this script. I think I’ll just abandon this all together…

I’d say it’s party resolved.
If you’re not getting errors about using Rigidbody as a type but instead it only throws errors when you’re trying to access the expected members, then there’s something wrong.

It basically means it knows a type called Rigidbody, but its declaration does not provide the member you’re trying to access. There must be a type in the scope that has a precedence to UnityEngine.Rigidbody unless your IDE does not complain about type-name abiguities.

In regards to the new warnings and errors, check the GameObject named ‘Tank’ and the TankManager component.
There should be some fields which have lost their references to actual instances of the specific types.
The error may be resolved once you’ve set the references properly.