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. Dismiss Notice

Running script problem

Discussion in 'Scripting' started by Czaroslaw, Apr 1, 2015.

  1. Czaroslaw

    Czaroslaw

    Joined:
    Apr 1, 2015
    Posts:
    2
    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:
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6. private CharacterController chCont;
    7. private CharacterMotor chMotor;
    8. private bool cursor;
    9.    
    10.     void Awake ()
    11.     {
    12.         cursor = true;
    13.         chCont = GetComponent<CharacterController>();
    14.         chMotor = GetComponent<CharacterMotor>();
    15.     }
    16.  
    17.     void FixedUpdate () {
    18.         if (Input.GetButtonDown ("Fire3"))
    19.         {
    20.             if (cursor == true)
    21.             {
    22.                 Screen.lockCursor = false;
    23.                 cursor = false;
    24.                 Debug.Log ("Cursor is unlocked");
    25.             }
    26.             else if (cursor == false)
    27.             {
    28.                 Screen.lockCursor = true;
    29.                 cursor = true;
    30.                 Debug.Log ("Cursor is locked");
    31.             }
    32.  
    33.         }
    34.    
    35.         if (Input.GetKey (KeyCode.W) & Input.GetKey (KeyCode.LeftShift) & chCont.isGrounded)
    36.         {
    37.             chMotor.movement.maxForwardSpeed = 24;  
    38.             Debug.Log (chMotor.movement.maxForwardSpeed);
    39.         }
    40.  
    41.         else if (Input.GetKey (KeyCode.W) & chCont.isGrounded)
    42.         {
    43.             chMotor.movement.maxForwardSpeed = 16;  
    44.             Debug.Log (chMotor.movement.maxForwardSpeed);
    45.         }
    46.  
    47.  
    48.     }
    49.  
    50.  
    51. }
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    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.
     
    Czaroslaw likes this.
  3. EgorHL570

    EgorHL570

    Joined:
    Apr 2, 2015
    Posts:
    2
    Всем привет! Помогите пожалуйста! Нашел скрипт в интернете, с помощью которого можно брать предметы как в Half Life 2, но он выдает ошибки:
    1 error CS0619: `UnityEngine.Component.camera' is obsolete: `Property camera has been deprecated. Use GetComponent<Camera>() 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. Помогите пожалуйста исправить скрипт или найти похожий.
     
  4. Czaroslaw

    Czaroslaw

    Joined:
    Apr 1, 2015
    Posts:
    2
    Thanks, It worked.