Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Will-D-Ramsey · May 01, 2015 at 04:09 AM · scripting problemunity5controller

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Will-D-Ramsey · May 04, 2015 at 09:29 AM 0
Share

Yeah no dice, I need an answer for how to access it in javascript.

avatar image jodev · May 04, 2015 at 10:22 AM 1
Share

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.

avatar image Will-D-Ramsey · May 04, 2015 at 11:35 AM 0
Share

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.

avatar image jodev · May 04, 2015 at 11:55 AM 1
Share

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

avatar image Will-D-Ramsey · May 04, 2015 at 12:19 PM 0
Share

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.:)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

why is it every single time i higher the number for the speed the speed of the ball stays the same? PLEASE HELP ITS SO FRUSTRATING!!!!!!!!!!! 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges