Running script problem

First, I’m gonna say I’m a total newbie in Unity.
I wanted to create a running script and most of things worked properly until I tried to make sprinting possible only when the character is on the ground. But when I tested it, Unity started printing errors saying:

MissingComponentException: There is no 'CharacterController' attached to the "Directional light" game object, but a script is trying to access it. You probably need to add a CharacterController to the game object "Directional light". Or your script needs to check if the component is attached before using it. Player.FixedUpdate () (at Assets/Scripts/Player.cs:35

What the hell is going on? Why CharacterController would be attached to Directional Light? I don’t get it.

Here’s my code:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

private CharacterController chCont;
private CharacterMotor chMotor;
private bool cursor; 
   
    void Awake ()
    {
        cursor = true;
        chCont = GetComponent<CharacterController>();
        chMotor = GetComponent<CharacterMotor>();
    }

    void FixedUpdate () {
        if (Input.GetButtonDown ("Fire3"))
        {
            if (cursor == true)
            {
                Screen.lockCursor = false;
                cursor = false;
                Debug.Log ("Cursor is unlocked");
            }
            else if (cursor == false)
            {
                Screen.lockCursor = true;
                cursor = true;
                Debug.Log ("Cursor is locked");
            }

        }
   
        if (Input.GetKey (KeyCode.W) & Input.GetKey (KeyCode.LeftShift) & chCont.isGrounded)
        {
            chMotor.movement.maxForwardSpeed = 24;   
            Debug.Log (chMotor.movement.maxForwardSpeed);
        }

        else if (Input.GetKey (KeyCode.W) & chCont.isGrounded)
        {
            chMotor.movement.maxForwardSpeed = 16;   
            Debug.Log (chMotor.movement.maxForwardSpeed);
        }


    }


}

In your scene find the directional light and remove script from it. If the light is your character then add a character control component instead.

1 Like

Всем привет! Помогите пожалуйста! Нашел скрипт в интернете, с помощью которого можно брать предметы как в Half Life 2, но он выдает ошибки:
1 error CS0619: UnityEngine.Component.camera' is obsolete: Property camera has been deprecated. Use GetComponent() instead. (UnityUpgradable)’

2
UnityEngine.Component’ does not contain a definition for ScreenPointToRay' and no extension method ScreenPointToRay’ of type `UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)
У меня Unitu5. Помогите пожалуйста исправить скрипт или найти похожий.

Thanks, It worked.