Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Crossgamerxt · Jan 02 at 08:34 PM · scripting problemscripting beginner

Toggling Script from another GameObject on/off Correctly using Unity Input System

I start the game being able to look and move around using a script called "Character_Control" attached to my "Player" gameobject. I can press escape to open a pause menu, and while I am in the pause menu, I want the "Character_Control" to be disabled so that I cannot control my player while the game is paused. I am using the unity input system.

I used the following code to toggle between the script being enabled/disabled, and it does toggle, however not the way I want it to:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class Test : MonoBehaviour
 {
     private DefaultInput defaultInput;
 
     public void Awake()
     {
         defaultInput = new DefaultInput();
         gameObject.GetComponent<Character_Control>().enabled = true;
     }
 
     private void OnEnable()
     {
         defaultInput.Enable();
     }
 
     private void OnDisable()
     {
         defaultInput.Disable();
     }
 
     void Update()
     {
         if (defaultInput.Character.Pause.triggered)
         {
             gameObject.GetComponent<Character_Control>().enabled = !gameObject.GetComponent<Character_Control>().enabled;
         }
     }
 
 }

I start the game with the "Character_Control" script enabled, and when I press escape (opening the pause menu), I can still control my character, but when I press escape for the second time (closing the pause menu), I cannot control my character, meaning the "Character_Control" script is disabled. Obviously I want the opposite to happen, so the "Character_Control" is disabled during pause.

Here is a video of my problem: https://drive.google.com/file/d/1hmtdMYcvW5tKhdBiCs-XklRZ75vpu81s/view?usp=sharing

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Jan 02 at 09:27 PM

Try:

 using UnityEngine;
 
 public class ToggleComponentWithInputSystem : MonoBehaviour
 {
     [SerializeField] Character_Control _target = null;
     DefaultInput _controls;
     void OnEnable ()
     {
         _controls = new DefaultInput();
         _controls.Character.Pause.performed += (ctx) => _target.enabled = !_target.enabled;
         _controls.Character.Pause.Enable();
 
         _target.enabled = true;
     }
     void OnDisable () => _controls.Dispose();
 }
Comment
Add comment · Show 2 · 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 Crossgamerxt · Jan 04 at 02:38 PM 0
Share

Tried it, attached the CharacterControl Script and I still have the same problem...

avatar image andrew-lukasik Crossgamerxt · Jan 04 at 04:06 PM 0
Share

That's ok. You can fix this with event then - I don't mean input events but:

 public event System.Action OnPaused;
 public event System.Action OnUnpaused;

to which your script could subscribe to be notified when game became paused/unpaused so there zero confusion about that in the code.

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.

Follow this Question

Answers Answers and Comments

144 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 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

I need Function to be in other script 1 Answer

Physics2D.OverlapCircle working in Unity but not in build 0 Answers

Get color from ColorPicker and apply it randomly on GameObject 0 Answers

How can I make a float decrease when touched by a cube? 0 Answers

How do I assign values to variables in the Update function only ONCE? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges