【问题标题】:Unity GetComponent causing errorUnity GetComponent 导致错误
【发布时间】:2015-07-19 08:32:09
【问题描述】:

所以我有一个非常基本的动画运动脚本,但我什至无法进入实际的动画部分,因为我遇到了这个错误:

Assets/Player Controllers/PlayerController.cs(18,41): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

直到今天它给我一个关于 GetComponent 的错误,但现在我什至无法复制它,尽管我没有更改任何一行代码。无论如何,这是完整的事情:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour{
public float runSpeed = 6.0F;
public float jumpHeight = 8.0F;
public float gravity = 20.0F;

private Vector3 moveDirection = Vector3.zero;

void Start(){
    controller = GetComponent<CharacterController>();
    animController = GetComponent<Animator>();
}

void Update(){
    if(controller.isGrounded){
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if(moveDirection = Vector3.zero){//Stopped
            isWalking = false;
            isBackpedaling = false;
        }else if(moveDirection = Vector3.back){//Backpedaling
            animController.isWalking = false;
            animController.isBackpedaling = true;
        }else{//Walking
            animController.isWalking = true;
            animController.isBackpedaling = false;
        }

        if(Input.GetButton("Jump")){
            moveDirection.y = jumpSpeed;
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    关于您遇到的错误,第 18 行:

    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    

    应该是:

    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    

    您应该进行的另一个修复(我假设是您之前的 GetComponent 错误的根源)是未声明您在 Start() 方法中分配的变量。通过添加到顶部来声明它们,如下所示:

    public class PlayerController : MonoBehaviour{
    public float runSpeed = 6.0F;
    public float jumpHeight = 8.0F;
    public float gravity = 20.0F;
    
    private Vector3 moveDirection = Vector3.zero;
    
    // Added
    private CharacterController controller;
    private Animator animController;
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      相关资源
      最近更新 更多