尝试创建运行脚本。访问播放器的CharacterMotor并提高移动速度,但编译器说这些对象不存在。 使用股票Fisrt人物角色控制器
错误CS1061:输入UnityEngine.Component' does not contain a definition for
CharacterMotor'并且找不到扩展方法CharacterMotor' of type
UnityEngine.Component'(您是否缺少using指令或程序集引用?)
public class PlayerRun : MonoBehaviour {
public GameObject _player;
private float _runSpeed = 10;
private float _defaultSpeed;
// Use this for initialization
void Start ()
{
_defaultSpeed = _player.GetComponent("CharacterMotor").CharacterMotor.maxForwardSpeed;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
_player.GetComponent("CharacterMotor").CharacterMotor.maxForwardSpeed = _runSpeed;
_player.GetComponent("CharacterMotor").CharacterMotor.maxSidewaysSpeed = _runSpeed;
_player.GetComponent("CharacterMotor").CharacterMotor.maxBackwardsSpeed = _runSpeed;
}
else
{
if (_player.GetComponent("CharacterMotor").CharacterMotor.maxForwardSpeed == _runSpeed)
{
_player.GetComponent("CharacterMotor").CharacterMotor.maxForwardSpeed = _defaultSpeed;
_player.GetComponent("CharacterMotor").CharacterMotor.maxSidewaysSpeed = _defaultSpeed;
_player.GetComponent("CharacterMotor").CharacterMotor.maxBackwardsSpeed = _defaultSpeed;
}
}
}
}
答案 0 :(得分:1)
看起来你刚从UnityScript转到C#?
您应该使用generic的GetComponent形式:
_defaultSpeed = _player.GetComponent<CharacterMotor>().maxForwardSpeed;
如果您致电GetComponent(typeof(T))
,您将获得Component
类型的引用。您可以将结果转换为T
,但为什么要这么麻烦?您可以轻松地调用GetComponent<T>()
来获取该类型的引用。