【发布时间】:2021-06-06 00:49:54
【问题描述】:
大家好,这是我第一次尝试在 Unity 中制作简单的游戏,但我卡在了移动脚本中,
我已经为我的播放器编写了脚本,称为“播放器”脚本,所以现在我正在尝试为我的控制器制作一个脚本,它是一个触摸屏板,例如向左、向右和跳跃,我正在关注 youtube 教程,
即使我的脚本和教程脚本相同,但我的有错误,它说我的 GameObject 没有找到或丢失...这是我的 pad 脚本
public class TouchPad :
MonoBehaviour {
public GameObject player;
public void OnMouseDown()
{
if (gameObject.name ==
"Left")
{
}
else if (gameObject.name ==
"Right")
{
}
else if (gameObject.name ==
"Jump")
{
}
}
public void OnMouseUp()
{
if (gameObject.name == "Left")
{
}
else if (gameObject.name ==
"Right")
{
}
else if (gameObject.name ==
"Jump")
{
}
}
public void OnMouseDrag()
{
if (gameObject.name ==
"Left")
{
player.GetComponent<player>.
().Left(); <<<<<<< The
<player> got "Could not be
Found"
}
else if (gameObject.name ==
"Right")
{
}
else if (gameObject.name ==
"Jump")
{
}
}
}
【问题讨论】:
-
不是您当前的问题,但
if (gameObject.name == "Lef")看起来像是一个错字。你的播放器类是player还是Player?您是否将播放器对象拖到检查器中的播放器插槽中? -
哦,不,对不起,它不是我的脚本中的错字,只是在这里,问题是 GetComponent
,,,你的意思是我需要将 player.script 拖到。 ..在哪里? -
它在哪一行准确地告诉你游戏对象丢失了?是这个脚本所在的那个吗?
-
"公共游戏对象播放器;"出错或丢失,是的,在那些脚本中
-
player.GetComponent<player>.().Left();无法编译。请编辑问题以包含minimal reproducible example。
标签: c# unity3d gameobject