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