• 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 theriser777 · 4 days ago · buttoncoroutinescriptableobjectkeycodeoptions

Can't modify ScriptableObject value.

So, I have a ScriptableObject which stores the KeyCodes for the player input:

InputController.cs:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     [CreateAssetMenu(fileName = "InputController", menuName = "ScriptableObjects/InputController", order = 1)]
     
     public class InputController : ScriptableObject
     {
         public KeyCode jump;
         public KeyCode left;
         public KeyCode right;
         public KeyCode shoot;
         public KeyCode hover;
         public KeyCode dash;
     }

The initial key values are assigned from the inspector, but I want to be able to change them from the options menu during runtime, so I created coroutine in another file, and that coroutine is called inside a function, which itself is assigned to a button.

The coroutine waits for me to press a key, and is supposed to assign that key to the corresponding action, in this case, jumping:

UI.cs:

 public List<InputController> warshControls = new List<InputController>();
 //This list stores the two ScritableObjects containing the control keys for the two players, assigned in the inspector.
 
         IEnumerator changeKey(KeyCode key)
         {   
         bool waitforInput=true;
         pressKeyText.text="Press a Key!";
         while(waitforInput)
         {
         foreach(KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))){
                 if(Input.GetKey(vKey)){
                     key=vKey;
                     waitforInput=false;
                     pressKeyText.text=" ";
                     Debug.Log(key);
                     Debug.Log(warshControls[0].jump);
                 }
             }  
             yield return null;  
         }
     }
     
     public void setPlayer1Jump()
     {
         StartCoroutine("changeKey", warshControls[0].jump);
     }
     //This is the function assigned to the button, it calls the coroutine to change the jumping key for Player1.

According to the Debug Logs, the value of the parameter "key" does change to whatever key I pressed, but for some reason the value of the argument I passed stays the same, meaning if the value of "warshControls[0].jump" was the key Z by default, and I called the coroutine and I pressed the key M, "key" would be M, while "warshControls[0].jump" would stay Z for some reason.

However, if I replace the line "key=vKey;" with "warshControls[0].jump=vKey;", the KeyCode value does actually change (And even persists after Play Mode is turned off which is a different problem entirely), but I want to be able to choose what KeyCode to modify, so I want this problem solved.

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

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

175 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Numpad Keypress not being registered on mobile phone. 0 Answers

How to switch JUMP BUTTON(SPACE KEY) into VIRTUAL BUTTON ON THE SCREEN? 2 Answers

Coroutine stops working after first run 1 Answer

assigning UI buttons to key 1 Answer

How To Make UI Button 2 Appear Three Seconds After UI Button 1 is pressed 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