- Home /
How to change the walk speed of the FPSController from another script?
I've been searching for an answer to this question for literal hours, and every answer is similar or the same, and it never works. I'm using the standard unity (version 2017.1.0f3) FPSController, and I'm trying to access the walkspeed and runspeed variables from another script. Almost every answer said that once you have the FirstPersonController saved as a variable, you can just do "controller.m_walkspeed = 0". This doesn't work for me. Whenever I try this, it highlights the "m_walkSpeed" or the "walkSpeed"*in red and gives me an
"error CS1061: Type 'UnityStandardAssets.Characters.FirstPerson.FirstPersonController' does not contain a definition for 'm_walkSpeed' and no extension method 'm_walkSpeed' of type 'UnityStandardAssets.Characters.FirstPerson.FirstPersonController' could be found. Are you missing an assembly reference?"
error. This seems to work for everybody else without a hitch, and I don't understand why it refuses to work for me. Any help would be appreciated. Here's the full code if that will help:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class pressButton : MonoBehaviour {
void Start() {
}
//joystick definitions
public FirstPersonController controller;
public Canvas reticule;
//button definitions
public Animator anim;
GameObject hitObject;
RaycastHit hitInfo;
void Update () {
Ray myRay = new Ray (transform.position, transform.forward);
if(Physics.Raycast (myRay, out hitInfo))
{
hitObject = hitInfo.collider.gameObject;
Debug.Log (hitObject.tag);
if (hitObject.tag == "button")
{
if (Input.GetMouseButtonDown (0))
{
anim = hitObject.GetComponent<Animator> ();
anim.SetTrigger ("pressButton");
}
}
if (hitObject.tag == "joystick") {
if (Input.GetMouseButtonDown (0))
{
reticule.enabled = false;
controller.m_walkSpeed = 0;
}
}
if (Input.GetMouseButtonUp (0) && reticule.enabled == false) {
reticule.enabled = true;
}
}
}
}
Have you actually looked into the code of the controller, checked what the name of variables actually are and what access modifier they're using?
Answer by GoneMadStudios · Mar 04, 2018 at 11:53 AM
I never used FirstPersonController
, so I may be wrong, but it seems that you have not referenced any FirstPersonController
, so you are trying to access the class itself, and not an instance of it.
If the FirstPersonController
is in the same GameObject as your pressButton
script, you should reference the controller in your Start()
function:
void Start() { controller = GetComponent<FirstPersonController>(); }
Okay, I feel really rediculously stupid. m_WalkSpeed has to be capitalized like that. I was typing m_walkSpeed. Thank you!
Answer by EddyDavila · Sep 19, 2018 at 07:04 AM
If it helps anyone, the problem is that the walkSpeed and runSpeed are declared as private variables in the FirstPersonController script. Just make them public as a quick fix.
It's strange, but how does unity editor know that private float m_WalkSpeed;
have to be shown in the properties explorer as "Walk speed" and also it's bold. $$anonymous$$aybe someone knows where the magic lives, and how can I use this WalkSpeed from outside without editing the standard class?
The m_WalkSpeed is private field but look at the the keyword SerializeField:
[SerializeField] private float m_WalkSpeed;