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.

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);
}