Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Cronabot · Jul 17, 2020 at 12:20 PM · inputinspectorvariablespublic variable

Public variable not showing in inspector using new Input System

Hi! I decided to have a go with the new input system and make a simple top down 2D movement script with it. The script worked fine using the old system but now that I have converted it to the new system I have an issue. The input system needs a reference to Input mappings but when I have a public (or private, serialized) variable the field will not show up in the inspector. Here is the code I have:

 public class PlayerMovement : MonoBehaviour
 {
 
     public float moveSpeed = 4f;
     public InputMaster controls;
 
     private Rigidbody2D rb;
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
 
         controls.PlayerMap.Movement.performed += ctx => MovePlayer(ctx.ReadValue<Vector2>());
     }
 
     private void OnEnable()
     {
         controls.Enable();
     }
 
     private void OnDisable()
     {
         controls.Disable();
     }
 
     private void MovePlayer(Vector2 direction)
     {
         rb.velocity = direction.normalized * moveSpeed;
 
         //TODO: Animate Sprite
     }
 }

I am getting no errors in the console and can't see anything wrong with my code. Thanks for the help!

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

2 Replies

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

Answer by Cronabot · Jul 17, 2020 at 08:21 PM

I had implemented the input system wrong because I was following an outdated tutorial. Whoops!

For anyone wondering, here is the working code:

 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerMovement : MonoBehaviour, InputMaster.IPlayerMapActions
 {
 
     public float moveSpeed = 4f;
 
     public InputMaster controls;
 
     private Rigidbody2D rb;
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     public void OnEnable()
     {
         if (controls == null)
         {
             controls = new InputMaster();
             controls.PlayerMap.SetCallbacks(this);
         }
         controls.PlayerMap.Enable();
     }
 
     public void OnMovement(InputAction.CallbackContext context)
     {
         Vector2 direction = context.ReadValue<Vector2>();
 
         rb.velocity = direction.normalized * moveSpeed;
 
         //TODO: Animate Sprite
     }
 }
 
Comment
Add comment · 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
0

Answer by art_maressa · May 04 at 02:04 PM

I spent way too long wondering what I was doing wrong before reading the comments on the brackeys tutorial.

video reference: https://www.youtube.com/watch?v=Pzd8NhcRzVo

comment by user cbox reads:

"TIP For those doing the tutorial more recently, you can no longer drag the file into the inspector as they changed the backend. You can just do controls = new InputMaster(); in awake to create a new instance of the object."

Essentially, IF your code looks like this:

using UnityEngine.InputSystem; using UnityEngine; public class ThirdPersonMovement : MonoBehaviour { public InputMaster controls; void Awake() { controls.Player.Movement.performed += _ => Move(); controls.Player.Shoot.performed += ctx => Shoot(); } void Move() { Debug.Log("Move"); } void Shoot() { Debug.Log("Shot"); } private void OnEnable() { controls.Enable(); } private void OnDisable() { controls.Disable(); } } Then you just need to add controls = new InputMaster(); in the awake function using UnityEngine.InputSystem; using UnityEngine; public class ThirdPersonMovement : MonoBehaviour { public InputMaster controls; void Awake() { // ADD NEW INSTANCE HERE--> controls = new InputMaster(); controls.Player.Movement.performed += _ => Move(); controls.Player.Shoot.performed += ctx => Shoot(); } void Move() { Debug.Log("Move"); } void Shoot() { Debug.Log("Shot"); } private void OnEnable() { controls.Enable(); } private void OnDisable() { controls.Disable(); } }

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

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

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

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Why aren't my editor values saving when I press play? 3 Answers

Debug inspector not showing private varibles! + Header issues 1 Answer

fixing a variable value during coroutine 1 Answer

Cannot assign Public GameObject variable in Inspector... 2 Answers


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