can't move the player

hi, i’m confused. i’m learning unity from here:

but why when i click the play button, i can’t move my player?

here’s the code:

// script for player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof (PlayerCtrller))]

public class Player_Scrpt : MonoBehaviour {
    public float move_speed = 5;
    PlayerCtrller controller;

    void Start () {
        controller = GetComponent<PlayerCtrller>();
    }
 
    void Update () {
        Vector3 move_input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 move_velocity = move_input.normalized * move_speed;
        controller.Move(move_velocity);
    }
}
// script for player controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof (Rigidbody))]

public class PlayerCtrller : MonoBehaviour {
    Rigidbody my_rbody;
    Vector3 velocity;
    void Start () {
        my_rbody = GetComponent<Rigidbody>();
    }

    public void Move(Vector3 v)
    {
        velocity = v;
    }

    public void FixedUpdate()
    {
        my_rbody.MovePosition(my_rbody.position + velocity * Time.fixedDeltaTime);
    }
}

the error msg says:
NullReferenceException: Object reference not set to an instance of an object Player_Scrpt.Update () (at Assets/Scripts/Player_Scrpt.cs:18) unity

can somebody tell me what’s wrong in my script?

Works for me…
Does your player have those both scripts attached?

1 Like

gee… i don’t know it should be attached both scripts… in the video, he just attached the player script only…
is it bcoz he’s using older / different unity version?

actually i had that problem also, it didnt automatically attach the required component,
if the script was already attached to the gameobject when you wrote it…

if you remove Player_Scrpt and attach it again, it will automatically add PlayerCtrller.cs also, because of this line:

[RequireComponent (typeof (PlayerCtrller))]

but how could that happen? so i should attach and detach it first to make it auto attach the related script the next time i attach it?