I need some help here. I am probably missing something but this script returns "Cannot convert UnityEngine.Component to CharacterMotorMovement. I am trying to access Unity's character controller so that I can implement sprinting to it. Here's my code: Code (csharp): using UnityEngine; using System.Collections; public class PlayerSprint : MonoBehaviour { public int sprintModifier; private CharacterMotorMovement movement; void Awake() { movement = (CharacterMotorMovement)gameObject.GetComponent("CharacterMotorMovement"); } void Update() { while(Input.GetKeyDown(KeyCode.LeftShift)) { movement.maxForwardSpeed = movement.maxForwardSpeed + sprintModifier; } movement.maxForwardSpeed = movement.maxForwardSpeed - sprintModifier; } }
Code (csharp): movement = (CharacterMotorMovement)gameObject.GetComponent<CharacterMotorMovement>(); or movement = gameObject.GetComponent<CharacterMotorMovement>() as CharacterMotorMovement; and why are you using a while loop ?
Same error in both cases: The type `CharacterMotorMovement' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.GameObject.GetComponent<T>()' I am officially stumped. LOL About the while loop, would it work correctly with an if statement?
change while(Input.GetKeyDown(KeyCode.LeftShift) to if(Input.GetKey(KeyCode.LeftShift))) otherwise you will create a infinite loop. and the problem is that there is no component called CharacterMotorMovement in the standart assets. there is only CharacterMotor
Well actually there is. It is a class in the CharacterMotor file. Isn't that registered as a component?