senox13
senox13

Reputation: 395

Unity GetComponent causing error

So I have a very basic animated movement script, but I can't even get to the actual animating part because I'm getting a this error:

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

up until today it was giving me an error about the GetComponent, but now I can't even replicate that, despite the fact that I haven't changed a single line of the code. Anyway, here's the full thing:

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);
}
}

Upvotes: 2

Views: 223

Answers (1)

John Boher
John Boher

Reputation: 158

In regards to the error you are getting, on line 18:

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

Should be:

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

Another fix you should make (I'm assuming was the source of your previous GetComponent error) is that the variables you assign in the Start() method aren't declared. Declare them by adding to the top part as follows:

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;

Upvotes: 3

Related Questions