- Home /
how can I change a toggle by pressing the F key?
Hello, Sorry but I kind of new to coding and I've been trouble shooting this for the last hour and I don't know whats wrong with it so I was wondering if you guys could help me, my script is:
bool IsFreeMode = false;
private void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
IsFreeMode = !IsFreeMode;
}
}
public void OnChangeValue ()
{
IsFreeMode = gameObject.GetComponentInChildren<Toggle>().isOn;
IsFreeMode = !IsFreeMode;
if (IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOn");
}
if (!IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOff");
}
Debug.Log("ValueChanged");
}
I'm trying to make it so I can make the Part in the Update method, This Part:
private void Update() { if(Input.GetKeyDown(KeyCode.F)) { IsFreeMode = !IsFreeMode; } }
I want to make it so the F key changes the toggle and changes it in the UI, Thanks!
Are you saying you want to call the method "OnChangeValue" when F is pressed? I'm a little bit confused here.
Yes, but I've tried calling it like you would any other void but it didnt work, but I also want the toggle to change too.
Call OnChangeValue something else, isn't that a special name?
Answer by Shreka · Aug 04, 2018 at 12:41 PM
Here copy this statments.
// Update is called once per frame
private void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
if (!IsFreeMode)
IsFreeMode = true;
else
IsFreeMode = false;
OnChangeValue();
}
}
public void OnChangeValue ()
{
gameObject.GetComponentInChildren<Toggle>().isOn = IsFreeMode;
if (IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOn");
}
if (!IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOff");
}
Debug.Log("ValueChanged");
}
also u should have a script in gameobject named player which has the method freemodeon an free modeoff
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//the script with free mode
public class script : MonoBehaviour{
public void FreeModeOn(){
Debug.Log("On");
}
public void FreeModeOff(){
Debug.Log("Off");
}
}
here its all corrected as u mgiht need @njwerner16. hope this fixes.
Thanks a lot but it was swapped so I made it:
if (!IsFree$$anonymous$$ode) { GameObject.Find("Player").Send$$anonymous$$essage("Free$$anonymous$$odeOn"); } if (IsFree$$anonymous$$ode) { GameObject.Find("Player").Send$$anonymous$$essage("Free$$anonymous$$odeOff"); }
but Thanks a lot! really helpful
Answer by Nobozarb · Aug 03, 2018 at 07:17 PM
I don't think the OnChangeValue() method is being called. You could probably replace the line IsFreeMode = !IsFreeMode;
with OnChangeValue();
in Update(), because it looks like the OnChangeValue() method does toggle the IsFreeMode variable, and also does all the other stuff you want.
Also, this isn't related to the question, but it is good practice to use GameObject.FindWithTag() instead of GameObject.Find(), and then set the tag of your player object to "Player" in the inspector, just to make sure your game doesn't break apart if another game object named "Player" somehow gets into the scene.
I tried that and it did not work but thanks for trying! If you know any other possible reason then let me know, thanks!
Can you explain what is going wrong? Is nothing happening or "ValueChanged" being logged but it isn't toggling or what?
If I understand correctly, the gameobject this script is attached to has children which have a toggle script with a variable isOn. Is the isOn variable a public variable or private variable? I believe it needs to public in order to be changed by another script.
but why u need a toggle to be switched with f key.
Is$$anonymous$$d simplify and call it from toggle script. So when ever u toggle it in runtime the fuction is called an freemode is set to on or off.
public void OnChangeValue ()
{
if (!IsFree$$anonymous$$ode){
IsFree$$anonymous$$ode = true;
GameObject.Find("Player").Send$$anonymous$$essage("Free$$anonymous$$odeOn");
}else{
IsFree$$anonymous$$ode = false;
GameObject.Find("Player").Send$$anonymous$$essage("Free$$anonymous$$odeOff");
}
gameObject.GetComponentInChildren<Toggle>().isOn = IsFree$$anonymous$$ode;
Debug.Log("ValueChanged");
}
Answer by ParkourTeddy · Aug 03, 2018 at 10:35 PM
I changed the OnChangeValue()
so that it instead of changing the bool IsFreeMode
to the toggles value, it first sets the new value of the toggle to gameObject.GetComponentInChildren<Toggle>().isOn = !gameObject.GetComponentInChildren<Toggle>().isOn
and after that changes the IsFreeMode to the new value of the toggle IsFreeMode = gameObject.GetComponentInChildren<Toggle>().isOn
.
This will change the value of the toggle in the UI when you press F. But pressing the toggle in the UI will not change in the IsFreeMode in the script.
bool IsFreeMode = false;
private void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
OnChangeValue();
}
}
public void OnChangeValue()
{
gameObject.GetComponentInChildren<Toggle>().isOn = !gameObject.GetComponentInChildren<Toggle>().isOn;
IsFreeMode = gameObject.GetComponentInChildren<Toggle>().isOn;
if (IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOn");
}
if (!IsFreeMode)
{
GameObject.Find("Player").SendMessage("FreeModeOff");
}
Debug.Log("ValueChanged");
}
It's not the best way to do it, but I thinkit might bring you one step closer to what you wanted.
I tried that and it did not work, it wouldn't change but thanks for trying!
Your answer
Follow this Question
Related Questions
Noob - Use the ESC, Shift and Command Key?? 1 Answer
Button to create object??? 1 Answer
Changing a code to accelerometer based? 0 Answers
How do I recover my license key? 1 Answer