- Home /
Hello All. How do you access the Run Speed, Jump Speed...ect variables in Unity 5s' new First Person Controller??
Hi im using java script and trying to access the FP Controller variables in Unity 5 im accessing the script just fine but im getting errors like "RunSpeed is not a member of object" please help me, I plan to hold an open beta in a month so this is urgent......."I NEED A HERO, IM HOLDING OUT FOR A HERO TILL THE MORNING LIGHT.." lol thanks and have a great day.
Answer by jodev · May 01, 2015 at 08:10 PM
Note: This post uses C#. I'm not well versed enough in Unity JavaScript to provide you with a conversion. That is up to you I'm affraid.
All of the fields you've mentioned are private fields within the FirstPersonController class. Not sure why. It wouldn't have been my choice to be honest. Anyway, the run speed is defined like this:
[SerializeField] private float m_RunSpeed;
The SerializeField attribute allows the field to be edited in the editor, but it won't be accessible in-game without the use of reflection. I'm not quite sure how well javascript handles reflection, but in C# you can access this field like this:
// Get the FirstPersonController script.
FirstPersonController person = GameObject.FindObjectOfType<FirstPersonController>();
// The step-by-step way.
Type personType = person.GetType();
FieldInfo runspeedFieldInfo = personType.GetField("m_RunSpeed", BindingFlags.NonPublic | BindingFlags.Instance);
float runSpeed = (float)runspeedFieldInfo.GetValue(person);
// The same thing in a single statement.
float runSpeed = (float)person.GetType()
.GetField("m_RunSpeed", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(person);
We first need to get a Type object of the FirstPersonController class. We then use this object to get information about the field that we want to query. These BindingFlags allow us to get access to non-public instance variable, such as m_RunSpeed. Finally we use the GetValue() method to get a value from an actual FirstPersonController instance (the FieldInfo is not tied to an instance, but to a type).
Note that reflection is slow, so you shouldn't use it in an Update() loop. Where possible, find the values in the Start() method, then cache them in a local variable.
Hope that helps.
EDIT: did some Googling and converted the short-form above to UnityScript:
import UnityStandardAssets.Characters.FirstPerson;
import System.Reflection;
function Start () {
var person : FirstPersonController;
person = GameObject.FindObjectOfType.<FirstPersonController>();
var runSpeed = person.GetType()
.GetField("m_RunSpeed", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(person);
}
Yeah no dice, I need an answer for how to access it in javascript.
Not going to lie, but but I'm a bit annoyed that you shot down a perfectly fine answer like that, just because it wasn't JavaScript. I was able to convert the answer to JavaScript in less then 15 $$anonymous$$utes, so you should have been able to.
I don't mean to sound angry or rude, but I feel you should at least put in a little bit of effort yourself.
I truly am sorry you feel that way but I am a beginner at C# and even more so to conversions between the two languages. I did find it helpful as I ended up changing the m_Runspeed within the FirstPersonController based on Input, even though this did not work perfectly it worked for the time being. I did spend a considerable amount of time on this problem, as it is planned to be a core mechanic in the use of Custom Classes (scout, heavy, solider, etc.) but never the less I failed in my endeavors. Again sorry for co$$anonymous$$g across as lazy or just biased towards Unityscript as this was not my intention, thank you and have a great day.
You know what the problem with internet communication is? It is so hard to convey the right emotion to go with a comment sometimes. It was the way you phrased your first comment that kind of rubbed me the wrong way. To me, it sounded like a snub, even though I'm sure you didn't intend for that to happen. Nor did I have any intention to imply you where lazy or didn't try. I realize I worded my reply quite poorly out of frustration. $$anonymous$$y sincere apologies for that.
That said, good luck with your project. If I come across more questions from you, I won't hesitate to look at it and provide an answer if I can. I can't promise it'll be in JavaScript though. ;)
Yeah your right there I think everyone has a little chat PTSD from Call of Duty, with all the wonders of technology we still have human error to account for. $$anonymous$$any thanks for the conversion and I welcome C# as my knowledge of it grows.:)
Your answer
Follow this Question
Related Questions
Problem with the camera, 2D. 0 Answers
The method StartHost() didn't work why ? 0 Answers
Character controller without Deltatime 1 Answer
Custom Character Controller Errors? 0 Answers