My component isn't getting recognised in the FixedUpdate.

I cant seem to get it to recognise the rb component is there any quick solution.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Player
    float moveSpeed = 4f;
    float speedlimiter = 0.7f;
    float inputHorizontal;
    float inputVertical;

    Rigidbody2D rb; // Components

    Vector2 movement;

    void Start()
    {
  
        rb = gameObject.GetComponent<Rigidbody2D>();

    }
    // Update is called once per frame
    void Update()
    {
        Input.GetAxisRaw("Horizontal");   // Function: pressing left gives -1. Pressing right 1 and zero for nothing
        Input.GetAxisRaw("Vertical");
 
    }
    void FixedUpdate ()
    {
        if (inputHorizontal != 0 || inputVertical != 0)  // This works because there is no jumping
        {
            if ((inputHorizontal != 0 && inputVertical != 0))
            {

                inputHorizontal *= speedlimiter;
                inputVertical *= speedlimiter;

            }

            rb.Velocity = new Vector2(inputHorizontal * moveSpeed, inputVertical * moveSpeed);
        }

        else
        {

            rb.Velocity = new Vector2(0f, 0f);

        }
    }
}

First, please use code-tags when posting code. Please edit your post above to use them. Code as plain text is awful to read. It also looks like your post has nothing to do with 2D, it’s a scripting question so should really be posted on the Scripting forum.

Input.GetAxisRaw("Horizontal"); // Function: pressing left gives -1. Pressing right 1 and zero for nothing
Input.GetAxisRaw("Vertical");

Seems you’re very new to C# because this code does nothing at all. You read the inputs here but you don’t store the results away anywhere. It seems you might be following a tutorial or some example code because you define these above it but don’t use them:

float inputHorizontal;
float inputVertical;

This statement doesn’t make sense. Maybe you mean FixedUpdate isn’t being called. But then you also say “I cant seem to get it to recognise the rb component is there any quick solution.” so it’s not clear what the problem is.

Please be specific in what you expect and what is actually happening. That said, the forums are not here to teach you the basic of C# and Unity, there are plenty of tutorials online including those in Unity Learn. I would recommend you try some. :slight_smile:

First, please make sure you use code tags when posting code, makes it much easier for people to help. Second, look at your declaration

Rigidbody2D rd;
rd = gameObject.GetComponent();

Then your usage

rb.Velocity = new Vector2(0f, 0f);

you change rd to rb!

Also, what MelvMay said, and please NO PICS OF CODE, please use code tage. It on the toolbar when you are creating a new post.

1 Like

To note, the code posted by the OP wouldn’t even compile. Maybe we’ll never see a reply but for the record, the velocity property (like all properties) is a lowercase V so Rigidbody2D.velocity. There’s too many problems with the code above.

Unity Learn Scripting