- Home /
Slow down character while reloading.
Hello guys! I want to slow down my character while he's reloading. That's my movement script :
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent.<CharacterController>();
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
That's the second script :
function Update()
{
if(boolReload==true)
GameObject.Find("SG_Player").GetComponent("weapon_preview").speed=2.0f;
}
But I get the following error : "BCE0019: 'speed' is not a member of 'UnityEngine.Component'". How can I fix it?
Answer by SteveFSP · Jun 17, 2015 at 08:12 PM
Basically what is happening is that Unity can't tell what type GetComponent() is supposed to return, so it assumes the type is UnityEngine.Component. Take a look at the examples in GameObject.GetComponent.
Your best option is to use the 'type' overload, rather than the 'string' overload like you did in your example. That will tell Unity what type to expect. So if 'weapon_preview' is the name of the first script, then simply leave out the quotes. So something like this:
GameObject.Find("SG_Player").GetComponent(weapon_preview).speed=2.0f; # No quotes.
If you need to use a string overload for some reason, then take a look at the second example in the API documentation I linked to above. So you'll need to do something like this:
var player : weapon_preview; # This lets unity know how to 'cast' the variable.
player = GameObject.Find("SG_Player").GetComponent("weapon_preview");
player.speed=2.0f;
Answer by Tekksin · Jun 17, 2015 at 08:11 PM
just realized your first script said you had the speed variable. The comment below mine is correct. I didn't realize you had quotes around the name.
Your answer

Follow this Question
Related Questions
auto run key 1 Answer
Slowing Down When Sliding Across Walls 2 Answers
Horizontal axis not working after animation 1 Answer
I am trying to make a movement script for my main camera but it isnt working! 2 Answers
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers