Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weird error need help

Discussion in 'Scripting' started by BasdeWildt, May 4, 2018.

  1. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    Unity gives this error :
    MissingComponentException: There is no 'CharacterController' attached to the "player" game object, but a script is trying to access it.
    You probably need to add a CharacterController to the game object "player". Or your script needs to check if the component is attached before using it.
    UnityEngine.CharacterController.Move (Vector3 motion) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/Physics/DynamicsBindings.gen.cs:3268)
    FPSController.Update () (at Assets/FPSController.cs:36)
    But i alreadyhave an component called player.

    I have screenshot attachet .Can someone help me with this. my code is
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class FPSController : MonoBehaviour {
    5.  
    6.     public float speed = 2f;
    7.     public float sensitivity = 2f;
    8.  
    9.     CharacterController player;
    10.  
    11.     float moveFB;
    12.     float moveLR;
    13.  
    14.     float rotX;
    15.     float rotY;
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.         player = GetComponent<CharacterController>();
    20.  
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         moveFB = Input.GetAxisRaw("Vertical") * speed;
    27.         moveLR = Input.GetAxisRaw("Horizontal") * speed;
    28.  
    29.         rotX = Input.GetAxis("Mouse X") * sensitivity;
    30.         rotY = Input.GetAxis("Mouse Y") * sensitivity;
    31.  
    32.         Vector3 movement = new Vector3(moveLR, 0, moveFB);
    33.         transform.Rotate(0, rotX, 0);
    34.  
    35.         movement = transform.rotation * movement;
    36.         player.Move(motion: movement * Time.deltaTime);
    37.  
    38.     }
    39. }
     
  2. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Hi
    You should reference to GameObject called player and this GameObject should have component CharacterController.
    Try like this:
    Code (CSharp):
    1. public GameObject player; (drag drop this gameobject in inspector)
    2. private CharacterController playerController;
    3. (if this player gameobject will have component attached then)
    4. void Start(){playerController = player.GetComponent<CharacterController>();}
    Now you are trying to call CharacterController component from the CharacterComponent (what is not good) and this CharacterController is not even set :)
    If this FpsController is attached to your player, then make sure this player gamobject has also CharacterController component.
     
    Last edited: May 4, 2018
  3. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    TomPo could you also look at my rotY,it doesnt move
     
  4. BasdeWildt

    BasdeWildt

    Joined:
    May 4, 2018
    Posts:
    13
    Could you look at my rotY, it doesnt work