'The variable player of FollowPlayer has not been assigned You probably need to assign the player variable of the FollowPlayer script in the inspector
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);
}
}
}
Solution 1:[1]
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.
Drag your player object from the scene hirarchy into this field.
This will assign a reference of the object in your scene to your public variable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


