Search code examples
c#unity-game-engineinputscene

Can i check if any F-Key is pressed?


Ok I want to check if any of the f keys are pressed:

if (Input.GetKeyDown(KeyCode.F1)||
    Input.GetKeyDown(KeyCode.F2)||
    Input.GetKeyDown(KeyCode.F3)||
    Input.GetKeyDown(KeyCode.F4)||
    ...){}

this seems very uneffective.

So i basically switch my Scenes when pressing f1, f2 etc. and i want to save the varibales to a global object when i'm leaving a scene.


Solution

  • I think the way your checking is alright.

    But global variable? I think you mean a Variable you can access from anywhere.

    Data persistence between scene's can be done in so many different way's I'll try to give u a Example

    So lets say your on Scene 1. You have a High Score you want to save. Static variables are great for Unity passthrews Imo atleast

    
        public class Score
        {
            public static int Highscore;
        }
    
    
    
        class Scene1
        {
            static void Main(string[] args)
            {
               if (Input.GetKeyDown(KeyCode.F1)||
                   Input.GetKeyDown(KeyCode.F2)||
                   Input.GetKeyDown(KeyCode.F3)||
                   Input.GetKeyDown(KeyCode.F4)||
                     ...){
                           Score.Highscore = 5;
                           Console.WriteLine("Highscore: " +  Score.Highscore);
                           SceneManager.LoadScene(0);
                         }
        //Output is = 5
            }
        }
    
    class Scene2
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine("Highscore: " +  Score.Highscore);
               //Output will still be 5
            }
        }
        ```