Okay, so I have a script and I’m trying to make the character crouch down. I have the crouching done, but now I need to change his speed when he’s crouched. Right now, I have:
gameObject.Find("First Person Controller").GetComponent("CharacterMotor").CharacterMotorMovement.maxForwardSpeed = 3;
It saying its a null reference exception. I’m thinking its because the variables I’m trying to access are in a class. How can I change the speed of him though code? Thanks
You don’t want to use CharacterMotorMovement because that’s the name of the class. The problem is, maxForwardSpeed isn’t a static variable. The CharacterMotorMovement object in your FPS Controller is actually an instance of that CharacterMotorMovement class simply called “movement.”
For example, I wrote a script that stores my player’s walking speed into a variable:
var player : Transform;
var walkingSpeed = player.GetComponent(“CharacterMotor”).movement.maxForwardSpeed;
If you still get errors, another thing you have to remember is that you can’t access maxForwardSpeed until after the game starts. So you might wanna put it in the Update() function or the Start() function.