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!
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 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.
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.