- Home /
How can I change the maxForwardSpeed variable in the CharacterMotor script form a different script?
I'm trying to make a sprinting script but I can't seem to figure out how to change that speed variable. I've tried changing the variable from inside the CharacterMotor script after spressing the shift key but nothing happens, that script confuses me.
Heres what I have. I've seen other questions like this but I still have not found my answer. With this script I get the error 'maxForwardSpeed' is nota member of 'UnityEngine.Component'
var motorScript = transform.GetComponent("CharacterMotor");
var sprintSpeed = 15.0;
function Start () {
}
function Update () {
if(Input.GetKey(KeyCode.LeftShift)){
motorScript.maxForwardSpeed = sprintSpeed;
}
}
Check this answer by Aldo for tapping into the Character $$anonymous$$otor : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html
motorScript.movement.maxForwardSpeed = speed;
Answer by liamcary · Feb 27, 2013 at 06:08 AM
I don't use JS, but judging from the error it thinks your "var motorScript" is of type Component, because GetComponent returns a Component type. If you explicitly declare motorScript as type CharacterMotor then you should be able to access CharacterMotor functions and variables. Try:
var motorScript : CharacterMotor;
motorScript = transform.GetComponent("CharacterMotor");
Your answer
Follow this Question
Related Questions
Whats a good sprinting script? 1 Answer
Why does it give me an error in this small java script for sprint? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Sprint cooldown(javascript) 2 Answers