How can i change variables in FirstPersonController scripts?
Hi. I creating pause menu in my game. When player click on whatever button in menu, cursor become hide. I have idea, how can i block cursor hide, but variables not changed. I use to help this site : http://answers.unity3d.com/questions/988322/how-can-i-access-firstpersoncontroller-script-vari.html
In my code have this: link to file:
public UnityStandardAssets.Characters.FirstPerson.MouseLook controller;
And changing variable when player press esc
controller.menuLocker = true;
Then in player controller i declared variable type bool and set it default as false. Like here:
public bool menuLocker = false;
My problem is, when i press esc and menu show me, variable is not switched. What i do wrong? Where i have problem? Thanks to all, who help me.
I created it in different way. I disabled lock cursor in mouseLook and create own. It working nicely.
Answer by PizzaPie · May 25, 2017 at 09:32 PM
Interesting, does that even get compiled? First of all controller is of MouseLook type so if you declared the menuLocker inside the FirstPersonController(gonna call it FPSc from now on) controller.menuLocker = true; should through a compiler error. Any way to get a reference(link) to the active instance of the FPSc you need to do something like this :
using UnityStandardAssets.Characters.FirstPerson; //this is to avoid long names
public class MyClass : MonoBehaviour {
//...
public FirstPersonController controller;
private MouseLook mLook; //this is if you need a reference to the MouseLook instance the FPSc is using
void Awake(){
mLook = controller.m_MouseLook; //this sets the reference
}
void Start(){
controller.menuLocker = true; //this will access the menuLocker field in the active FPSc
}}
of course you need to put it on an GameObject inside the scene and then drag and drop the GameObject that contains the FPSc on the field controller. Also you need to go to the FPSc script find the m_MouseLook field and change it to public.
Note: the easiest way to stop this Cursor behaviour is to just disable the FPSc component you have active in scene while you are on menu and re enable it afterwards.
Note2: keep in mind that Unity in edit mode( inside editor) have a safenet that causes the cursor to get unlocked with esc regardless of your scripts, which might cause some funky behaviours some times. Doesn't happen on builds.
Note3: watch out for the UpdateCursorLock() inside MouseLook class because it is the culprit that relocks the cursor on click and it is called internally inside LateUpdate on MouseLook and somewhere inside the FPSc script, quite annoying approach if you ask me.
I don't understand you very much. I try your code and nothing. Became some errors, thats all. I try too set value via method inside $$anonymous$$ouse Locker, but still not working. I don't know why.
public void Change$$anonymous$$enuLocker(bool state){
menuLocker = state;
Debug.Log ("Provedlo se");
}
controller.Change$$anonymous$$enuLocker (false); // in menu script
Thaks
How do you set the reference to the controller? $$anonymous$$anually through Editor or on Runtime or you use new keyword? Is the menuLocker located inside $$anonymous$$ouseLook or inside FirstPersonContoller class? Please post your class that tries to manipulate the others. Cheers.
Answer by Marks98 · May 30, 2017 at 04:44 PM
I set reference to MouseLook manually, and yes, the Mouse Look is inside prefab first player controller. public UnityStandardAssets.Characters.FirstPerson.MouseLook controller; // reference to file
public void GameMenuBackGame(){ //if player click on the button
gameMenu.enabled = false;
controller.ChangeMenuLocker (true);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (gameMenu.enabled == false) {
gameMenu.enabled = true;
controller.ChangeMenuLocker (false);
}
}
}
This is all code, where i trying manipuled with variable. Thanks
You sure you drag and drop the Object with the $$anonymous$$ouseLook from your hierarchy and not the Assets? But i am quite sure there is no $$anonymous$$ouseLook Component on the FirstPersonController prefab, maybe they changed it on the last Update.
I don't know where is problem. $$anonymous$$abye is better for me write own mouse look script.
Your answer
