Home > Blockchain >  The variable player of FollowPlayer has not been assigned You probably need to assign the player var
The variable player of FollowPlayer has not been assigned You probably need to assign the player var

Time:04-02

I was just making a game in unity and watching a tutorial from youtube. When all of a sudden an error in the console said

"The variable player of FollowPlayer has not been assigned You probably need to assign the player variable of the FollowPlayer script in the inspector."

How do you fix it?

Code:

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    public Transform player;
    // Update is called once per frame
    void Update()
    {
        Debug.Log(player.position);
    }
}

And the other c# file has some code in it, it might have something to do with the error.

Second c# code:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{ public Rigidbody rb;



    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;
    
    void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime); 

        if (Input.GetKey("d"))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
        }
        if (Input.GetKey("a"))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
        }
    }
}

CodePudding user response:

FollowPlayer has a public varaible.

The inspector of FollowPlayer object will have a field in the inspector where you can give this variable a value.

Empty inspector field

Drag your player object from the scene hirarchy into this field.

Filled inspector field

  • Related