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 /
  • Help Room /
avatar image
0
Question by DanuVIP · Mar 08, 2021 at 10:04 PM · scripting problemconfusedstatic variable

Hey, I'm working on an input system, but somehow it doesn't work anymore

I worked on and completed this Tutorial the day before, today I wanted to implement it in my real game and suddenly it didn't work anymore, somehow the scripts no longer want to communicate with each other even though I only added a few variables. There is a: public static GameManager GM;

here are the Scripts: Number 1 = GameManager.cs

 public class GameManager : MonoBehaviour
 {
 
     public static GameManager GM;
 
     public KeyCode drift { get; set; }
     public KeyCode forward { get; set; }
     public KeyCode backward { get; set; }
     public KeyCode left { get; set; }
     public KeyCode right { get; set; }
     public KeyCode restart { get; set; }
     public KeyCode menu { get; set; }
     public KeyCode item { get; set; }
 
     void Awake()
     {
         if (GM == null)
         {
             DontDestroyOnLoad(gameObject);
             GM = this;
         }
         else if (GM != this)
         {
             Destroy(gameObject);
         }
         
         drift = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("driftKey", "Space"));
         forward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("forwardKey", "W"));
         backward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("backwardKey", "S"));
         left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("leftKey", "A"));
         right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("rightKey", "D"));
         restart = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("restartKey", "R"));
         menu = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("menuKey", "M"));
         item = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("itemKey", "LeftControl"));
 
     }
 }

and number 2 is the UI Menu Script also in C#

 public class Controlsscript : MonoBehaviour
 {
     Transform menuPanel;
     Event keyEvent;
     Text buttonText;
     KeyCode newKey;
 
     bool waitingForKey;
 
     // Start is called before the first frame update
     void Start()
     {
         menuPanel = transform.Find("Panel");
         menuPanel.gameObject.SetActive(false);
         waitingForKey = false;
 
         for (int i = 0; i < 8; i++)
         {
             
             if (menuPanel.GetChild(i).name == "ForwardKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.forward.ToString();
             if (menuPanel.GetChild(i).name == "BackwardKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.backward.ToString();
             if (menuPanel.GetChild(i).name == "LeftKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.left.ToString();
             if (menuPanel.GetChild(i).name == "RightKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.right.ToString();
             if (menuPanel.GetChild(i).name == "DriftKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.drift.ToString();
             if (menuPanel.GetChild(i).name == "RestartKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.restart.ToString();
             if (menuPanel.GetChild(i).name == "MenuKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.restart.ToString();
             if (menuPanel.GetChild(i).name == "ItemKey")
                 menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.GM.restart.ToString();
 
 
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         
         if (Input.GetKeyDown(KeyCode.Escape) && !menuPanel.gameObject.activeSelf)
             menuPanel.gameObject.SetActive(true);
         else if (Input.GetKeyDown(KeyCode.Escape) && menuPanel.gameObject.activeSelf)
             menuPanel.gameObject.SetActive(false);
         
     }
 
     void OnGUI()
     {
         keyEvent = Event.current;
 
         if (keyEvent.isKey && waitingForKey)
         {
             newKey = keyEvent.keyCode;
             waitingForKey = false;
         }
     }
 
     public void StartAssignment(string keyName)
     {
         if (!waitingForKey)
         {
             StartCoroutine(AssignKey(keyName));
         }
     }
 
     public void SendText(Text text)
     {
         buttonText = text;
     }
 
     IEnumerator WaitForKey()
     {
         while (!keyEvent.isKey)
         {
             yield return null;
         }
     }
 
     public IEnumerator AssignKey(string keyName)
     {
         waitingForKey = true;
 
         yield return WaitForKey();
 
         switch (keyName)
         {
             case "forward":
                 GameManager.GM.forward = newKey;
                 Debug.Log("Neuer Key");
                 buttonText.text = GameManager.GM.forward.ToString();
                 PlayerPrefs.SetString("forwardKey", GameManager.GM.forward.ToString());
                 break;
             case "backward":
                 GameManager.GM.backward = newKey;
                 buttonText.text = GameManager.GM.backward.ToString();
                 PlayerPrefs.SetString("backwardKey", GameManager.GM.backward.ToString());
                 break;
             case "left":
                 GameManager.GM.left = newKey;
                 buttonText.text = GameManager.GM.left.ToString();
                 PlayerPrefs.SetString("leftKey", GameManager.GM.left.ToString());
                 break;
             case "right":
                 GameManager.GM.right = newKey;
                 buttonText.text = GameManager.GM.right.ToString();
                 PlayerPrefs.SetString("rightKey", GameManager.GM.right.ToString());
                 break;
             case "drift":
                 GameManager.GM.drift = newKey;
                 buttonText.text = GameManager.GM.drift.ToString();
                 PlayerPrefs.SetString("driftKey", GameManager.GM.drift.ToString());
                 break;
             case "restart":
                 GameManager.GM.restart = newKey;
                 buttonText.text = GameManager.GM.restart.ToString();
                 PlayerPrefs.SetString("restartKey", GameManager.GM.restart.ToString());
                 break;
             case "menu":
                 GameManager.GM.menu = newKey;
                 buttonText.text = GameManager.GM.menu.ToString();
                 PlayerPrefs.SetString("menuKey", GameManager.GM.menu.ToString());
                 break;
             case "item":
                 GameManager.GM.item = newKey;
                 buttonText.text = GameManager.GM.item.ToString();
                 PlayerPrefs.SetString("itemKey", GameManager.GM.item.ToString());
                 break;
         }
         yield return null;
     }
 
     public void resetAllKeybinds()
     {
         PlayerPrefs.DeleteKey("forwardKey");
         PlayerPrefs.DeleteKey("backwardKey");
         PlayerPrefs.DeleteKey("leftKey");
         PlayerPrefs.DeleteKey("rightKey");
         PlayerPrefs.DeleteKey("driftKey");
         PlayerPrefs.DeleteKey("restartKey");
         PlayerPrefs.DeleteKey("menuKey");
         PlayerPrefs.DeleteKey("itemKey");
     }
 }
 

and somehow they communicated yesterday and today they don't want to (I just changed on or two Variable names and added a bit not changed anything important)

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 DanuVIP · Nov 17, 2021 at 01:45 PM

still searching for a solution..

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

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

Game freezes when restarting the game 0 Answers

..Object reference not set to an instance of an object.. Error occuring for a public static script variable. 1 Answer

Script attached on prefab; cannot attach "text" component 1 Answer

How can I get the player to stay on the moving platform? (2D platformer) 1 Answer

i'm getting error CS1014 when putting a C# scripting into something. 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