FollowPlayer 的变量 player 没有赋值 你可能需要在 inspector 中赋值 FollowPlayer 脚本的 player 变量
The variable player of FollowPlayer has not been assigned You probably need to assign the player variable of the FollowPlayer script in the inspector
我只是用 unity 制作游戏并观看了 youtube 上的教程。当控制台突然出现一个错误时
"The variable player of FollowPlayer has not been assigned You probably need to assign the player variable of the FollowPlayer script in the inspector."
你是怎么解决的?
代码:
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
// Update is called once per frame
void Update()
{
Debug.Log(player.position);
}
}
而另一个c#文件中有一些代码,可能与错误有关。
第二个 c# 代码:
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);
}
}
}
FollowPlayer 有一个 public 变量。
FollowPlayer
对象的检查器将在检查器中有一个字段,您可以在其中为该变量赋值。
将您的播放器对象从场景层次结构拖到此字段中。
这会将场景中对象的引用分配给 public 变量。
我只是用 unity 制作游戏并观看了 youtube 上的教程。当控制台突然出现一个错误时
"The variable player of FollowPlayer has not been assigned You probably need to assign the player variable of the FollowPlayer script in the inspector."
你是怎么解决的?
代码:
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
// Update is called once per frame
void Update()
{
Debug.Log(player.position);
}
}
而另一个c#文件中有一些代码,可能与错误有关。
第二个 c# 代码:
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);
}
}
}
FollowPlayer 有一个 public 变量。
FollowPlayer
对象的检查器将在检查器中有一个字段,您可以在其中为该变量赋值。
将您的播放器对象从场景层次结构拖到此字段中。
这会将场景中对象的引用分配给 public 变量。