• 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 njwerner16 · Aug 03, 2018 at 03:08 PM · key

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!

Comment
Add comment · Show 4
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 cryingwolf85 · Aug 03, 2018 at 04:22 PM 0
Share

Are you saying you want to call the method "OnChangeValue" when F is pressed? I'm a little bit confused here.

avatar image njwerner16 cryingwolf85 · Aug 03, 2018 at 05:37 PM 0
Share

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.

avatar image bennett_apps njwerner16 · Aug 03, 2018 at 07:02 PM 0
Share

Call OnChangeValue something else, isn't that a special name?

Show more comments

3 Replies

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

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.

Comment
Add comment · Show 1 · 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 njwerner16 · Aug 04, 2018 at 05:31 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 4 · 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 njwerner16 · Aug 03, 2018 at 07:24 PM 0
Share

I tried that and it did not work but thanks for trying! If you know any other possible reason then let me know, thanks!

avatar image Nobozarb njwerner16 · Aug 04, 2018 at 12:06 PM 0
Share

Can you explain what is going wrong? Is nothing happening or "ValueChanged" being logged but it isn't toggling or what?

avatar image Nobozarb njwerner16 · Aug 04, 2018 at 02:52 PM 1
Share

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.

avatar image Shreka Nobozarb · Aug 04, 2018 at 02:58 PM 0
Share

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");
      } 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 njwerner16 · Aug 04, 2018 at 12:55 AM 0
Share

I tried that and it did not work, it wouldn't change but thanks for trying!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is GetKeyDown / Up behaving erratically, only responding occasionally in build? 1 Answer

Open door with key 2 Answers

Animation controller 2 Answers

Interact with doors using a key? 1 Answer

Play sound, while press any key 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges