Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

How do I define Move?

Discussion in 'Scripting' started by Cocoa678, Aug 10, 2020.

  1. Cocoa678

    Cocoa678

    Joined:
    Aug 10, 2020
    Posts:
    4
    Okay, so I'm watching a tutorial with nearly 80k views that told me what to code, but when I run this it says there is no definition for "Move." I know it wasn't given one, but the tutorial's code has no errors for some reason? I triple checked for errors in spelling but there are none and it still doesn't work. How do I give Move a proper defenition?



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterController : MonoBehaviour {
    6.  
    7.     public float moveSpeed;
    8.  
    9.     public float jumpForce;
    10.  
    11.     public CharacterController controller;
    12.  
    13.     private Vector3 moveDirection;
    14.    
    15.     // Start is called before the first frame update
    16.     void Start() {
    17.        
    18.     controller = GetComponent<CharacterController>();
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update() {
    24.        
    25.     moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0f, Input.GetAxis("Vertical") * moveSpeed);
    26.  
    27.     if (Input.GetButtonDown("Jump")) {
    28.  
    29.       moveDirection.y = jumpForce;
    30.  
    31.     }
    32.  
    33.     controller.Move(moveDirection * Time.deltaTime);
    34.  
    35.     }
    36. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
  3. Cocoa678

    Cocoa678

    Joined:
    Aug 10, 2020
    Posts:
    4
    Here's the error message:

    Assets\CharacterController.cs(33,16): error CS1061: 'CharacterController' does not contain a definition for 'Move' and no accessible extension method 'Move' accepting a first argument of type 'CharacterController' could be found (are you missing a using directive or an assembly reference?)
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Oh so... did you make your own script called CharacterController? I was assuming you were using the builtin version from Unity: https://docs.unity3d.com/ScriptReference/CharacterController.html

    Did they make their own custom CharacterController class in the tutorial?

    Edit: Duh - just looked at your script. Try renaming your script to something else, because Unity has a CharacterController already and I think you're probably running into trouble because of that.
     
  5. Cocoa678

    Cocoa678

    Joined:
    Aug 10, 2020
    Posts:
    4
    I renamed the script but am still getting the error. The tutorial does have a CharacterController class, I copied their code word for word.


    Edit: I am also using the built in CharacterController for my Player. I think the problem might be that I am not referring back to it?
     
    Last edited: Aug 11, 2020
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You can make sure you're using Unity's built in character controller by changing the variable declaration to this:
    Code (CSharp):
    1. public UnityEngine.CharacterController controller;
    and changing the line in Start to this:
    Code (CSharp):
    1. controller = GetComponent<UnityEngine.CharacterController>();
    But really you'll have a much easier time if the names are different. Would you mind sharing which tutorial you're following?
     
    Cocoa678 and mgrekt like this.
  7. Cocoa678

    Cocoa678

    Joined:
    Aug 10, 2020
    Posts:
    4