MissingComponentException?

Oleg

New member
Joined
Jul 1, 2022
Messages
3
Programming Experience
Beginner
hello I have a problem please help
MissingComponentException: There is no 'Rigidbody' attached to the "player " game object, but a script is trying to access it.You probably need to ad
C#:
using яSystem.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerScript: MonoBehaviour
{
    public float speed;
    private Rigidbody rb;
    float MoveHorizontal;
    float MoveVertical;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }


    void FixedUpdate()
    {
        Vector3 movement = new Vector3(MoveHorizontal, 0.0f, MoveVertical);

        rb.AddForce(movement * speed);
    }

    public void Up()
    {
        MoveVertical = 1f;
    }

    public void Down()
    {
        MoveVertical = -1f;
    }

    public void StopMoveVertical()
    {
        MoveVertical = 0f;
    }

    public void Left()
    {
        MoveVertical = 1f;
    }

    public void Right()
    {
        MoveHorizontal = -1f;
    }

    public void StopMoveHorizontal()
    {
        MoveHorizontal = 0f;
    }
}
 
Last edited by a moderator:
So you posted your code, but you didn't tell us what your problem is. Also, you've not told us what you have already tried to solve your problem.
 
Back
Top Bottom