- Home /
Using C# and getcomponent...
I have a First Person Controller from the Unity Assets I imported. I'd like to know in C#, how to access the CharacterMotor Component's 'Max Forward Speed' variable from another script, as well as change it...
$$anonymous$$ore information needed. What scrip defines 'Forwardmax', and what game object is it attached to?
Forward max speed is A movement thingy (Its max forward speed.)made by the first person controller script. Its in the first person controller. I would just like to see a script that finds this, can maybe alter it too.
Did you read the comments to your other question on the same subject...?
Answer by clunk47 · Nov 26, 2013 at 12:02 AM
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
//Create a motor variable.
CharacterMotor motor;
//Create your own speed variable.
float mfs;
//For testing purposes
string label;
void Start ()
{
//Be sure component is found before assigning.
if(GetComponent<CharacterMotor>())
motor = GetComponent<CharacterMotor>();
//Check if motor is null or not before matching your speed with motor's speed.
//If you want to create your own speed on startup, just assign a float or int to mfs and remove this.
if(motor != null)
mfs = motor.movement.maxForwardSpeed;
}
void Update()
{
//Press and hold up or down on vertical axis to increase and decrease speed.
//For Testing Purposes.
mfs += (int)Input.GetAxis ("Vertical");
//Be sure motor is not null before matching motor's speed with your variable's speed.
if(motor)
motor.movement.maxForwardSpeed = mfs;
//Update label at runtime to output GUI.
//For Testing Purposes.
label = "Motor Script Max Forward Speed = " + motor.movement.maxForwardSpeed + "\n" + "Your Max Forward Speed = " + mfs;
}
void OnGUI ()
{
//Print label variable.
GUILayout.Label (label);
//Instructions.
GUILayout.Label("\n" + "Press Up and Down Arrow keys (or W and S) to increase or decrease Max Forward Speed.");
}
}
Dude. This is an example for you to copy and paste lol. If you want to simply access it without all the extra:
float maxSpeed;
void Start()
{
maxSpeed = GetComponent<Character$$anonymous$$otor>().movement.maxForwardSpeed;
}
Really simple.
If its soooo simple, why does it error out, half the crap is red.
Because you're not doing something right. The C-SHARP Example I posted above in my original answer has no errors. Attach it to your First Person Controller. First Person Controller should have Character $$anonymous$$otor attached. Be sure you're creating a C# script, not a Javascript and pasting this into it. Be sure you name your C# script Example in this case, since my class name is 'Example'. It IS really simple. You just need to be willing to learn, and when you have an issue, give details. "Half the crap" doesn't tell me a damn thing. I'm willing to help if you're willing to put some effort forth in asking a question.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Bullet not moving from script 3 Answers