- Home /
Dynamically Changing the Character Motor Script
I'm Trying to get change the max forward speed of the character motor script but I'm not sure how to access the variables. I've tried:
gameObject.GetComponent(CharacterMotor).maxForwardSpeed=gameObject.GetComponent(CharacterMotor).maxForwardSpeed * (((speed)/100.0)+1);
However, this doesn't seem to work at all, and printing:
gameObject.GetComponent(CharacterMotor).maxForwardSpeed);
Returns null. Any idea what I'm doing wrong?
Answer by Simple · Feb 07, 2011 at 01:20 PM
As you see this parameter is in class CharacterMotorMovement
class CharacterMotorMovement { // The maximum horizontal speed when moving var maxForwardSpeed : float = 10.0; var maxSidewaysSpeed : float = 10.0; var maxBackwardsSpeed : float = 10.0; //... }
var movement : CharacterMotorMovement = CharacterMotorMovement();
So if you want to get parameter member of class you should do this:
Just use .movement.maxForwardSpeed instead of maxForwardSpeed
var motor = gameObject.GetComponent("CharacterMotor");
var max_speed = motor.movement.maxForwardSpeed;
max_speed = max_speed*(((speed)/100.0)+1);
motor.movement.maxForwardSpeed = max_speed;
or just
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed =
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed*(((speed)/100.0)+1);
Well done, but do you know the *= operator? :D Would be much easier ;)
I know. For somebody this operator could not be clear. I used multiplication sign just as in author example code.
Your answer
Follow this Question
Related Questions
Variable Resolution 2 Answers
Dynamic reference of a Class variable 1 Answer
Adding variables to a class at runtime 1 Answer
Dynamically Added Listener to UI Button Not Calling With Variable Parameters 1 Answer
Dynamic variable name c# 1 Answer