Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I make a random sequence of keys to press?

Discussion in 'Editor & General Support' started by b1rso, Aug 6, 2018.

  1. b1rso

    b1rso

    Joined:
    Jul 7, 2018
    Posts:
    19
    Hi, there! So, I want the player to press a sequence of keys that is randomly generated by the game.
    In my case, I want the sequence to be with 6 keys and only with the arrow keys. What I mean is like:
    Randomly generates sequence > The sequence is UP, UP, DOWN, LEFT, UP, RIGHT > The player needs to do this sequence.
    What I tried so far was making this:
    Code (CSharp):
    1. public KeyCode first, second, third, fourth, fifth, sixth;
    2.     private KeyCode[] sequence = new KeyCode[]
    3.     {
    4.         first,
    5.         second,
    6.         third,
    7.         fourth,
    8.         fifth,
    9.         sixth
    10.     };
    11.     private int sequencePattern;
    12.  
    13.     void Start () {
    14.  
    15.     }
    16.    
    17.     void Update () {
    18.  
    19.         float one = Random.Range (0, 4);
    20.         float two = Random.Range (0, 4);
    21.         float three = Random.Range (0, 4);
    22.         float four = Random.Range (0, 4);
    23.         float five = Random.Range (0, 4);
    24.         float six = Random.Range (0, 4);
    25.  
    26.         if (one == 0) {
    27.             first = KeyCode.UpArrow;
    28.         } else if (one == 1) {
    29.             first = KeyCode.DownArrow;
    30.         } else if (one == 2) {
    31.             first = KeyCode.LeftArrow;
    32.         } else if (one == 3) {
    33.             first = KeyCode.RightArrow;
    34.         }
    35.  
    36.         if (two == 0) {
    37.             second = KeyCode.UpArrow;
    38.         } else if (two == 1) {
    39.             second = KeyCode.DownArrow;
    40.         } else if (two == 2) {
    41.             second = KeyCode.LeftArrow;
    42.         } else if (two == 3) {
    43.             second = KeyCode.RightArrow;
    44.         }
    45.  
    46.         if (three == 0) {
    47.             third = KeyCode.UpArrow;
    48.         } else if (three == 1) {
    49.             third = KeyCode.DownArrow;
    50.         } else if (three == 2) {
    51.             third = KeyCode.LeftArrow;
    52.         } else if (three == 3) {
    53.             third = KeyCode.RightArrow;
    54.         }
    55.  
    56.         if (four == 0) {
    57.             fourth = KeyCode.UpArrow;
    58.         } else if (four == 1) {
    59.             fourth = KeyCode.DownArrow;
    60.         } else if (four == 2) {
    61.             fourth = KeyCode.LeftArrow;
    62.         } else if (four == 3) {
    63.             fourth = KeyCode.RightArrow;
    64.         }
    65.  
    66.         if (five == 0) {
    67.             fifth = KeyCode.UpArrow;
    68.         } else if (five == 1) {
    69.             fifth = KeyCode.DownArrow;
    70.         } else if (five == 2) {
    71.             fifth = KeyCode.LeftArrow;
    72.         } else if (five == 3) {
    73.             fifth = KeyCode.RightArrow;
    74.         }
    75.  
    76.         if (six == 0) {
    77.             sixth = KeyCode.UpArrow;
    78.         } else if (six == 1) {
    79.             sixth = KeyCode.DownArrow;
    80.         } else if (six == 2) {
    81.             sixth = KeyCode.LeftArrow;
    82.         } else if (six == 3) {
    83.             sixth = KeyCode.RightArrow;
    84.         }
    85.        
    86.     }
    87. }
    And I keep receiving the error "A field initializer cannot reference the nonstatic field, method, or property `NameOfTheClass.first', `NameOfTheClass.second'" and so on.

    Thanks ^^
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    In regards to the error you're getting, the issue is that this:

    Code (CSharp):
    1.     private KeyCode[] sequence = new KeyCode[]
    2.     {
    3.         first,
    4.         second,
    5.         third,
    6.         fourth,
    7.         fifth,
    8.         sixth
    9.     };
    Is not allowed to access these from the class scope (not inside a method):

    Code (CSharp):
    1. public KeyCode first, second, third, fourth, fifth, sixth;
    In order to assign
    sequence
    to be a sequence of the six keycodes, you would need to create that array in Start (or some other method).

    However, this won't solve your overall problem. KeyCode is an enum. Enums are passed by value. If you created an array and put
    first
    in it for example, it would not be putting the variable
    first
    in the array, it would be putting the value of
    first
    into the array. Meaning that if
    first
    gets changed later, the array value won't update to match it. So with that logic, you would need to assign the generated keys to the array every time you regenerate them (and putting that logic in Update seems like a bad idea unless you want to generate a new sequence every frame).

    I wouldn't approach this problem from such a hardcoded angle. I would probably just do something like this:

    Code (CSharp):
    1. public KeyCode[] validSequenceKeys = new [] {
    2.     KeyCode.UpArrow,
    3.     KeyCode.RightArrow,
    4.     KeyCode.DownArrow,
    5.     KeyCode.LeftArrow
    6. };
    7.  
    8. KeyCode[] GenerateSequence ( int length = 6 ) {
    9.     KeyCode[] sequence = new KeyCode[ length ];
    10.  
    11.     for( int i = 0; i < length; i++ ) {
    12.         var key = validSequenceKeys[ Random.Range( 0, validSequenceKeys.Length ) ];
    13.         sequence[ i ] = key;
    14.     }
    15.  
    16.     return sequence;
    17. }
     
    b1rso likes this.
  3. b1rso

    b1rso

    Joined:
    Jul 7, 2018
    Posts:
    19
    Got it, thanks a lot!

     
  4. vipergabe

    vipergabe

    Joined:
    Jan 28, 2021
    Posts:
    5
    I'm pretty new to unity and the code is helpful but any help on how to implement it to actually get a display would be really appreciated
     
  5. Uthgaard

    Uthgaard

    Joined:
    Dec 17, 2020
    Posts:
    25
    Create a Text UI element and make the key name to be part of the text displayed
     
  6. vipergabe

    vipergabe

    Joined:
    Jan 28, 2021
    Posts:
    5
    I get these errors:
    Assets\Arrows.cs(5,18): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\Arrows.cs(12,11): error CS0116: A namespace cannot directly contain members such as fields or methods
    Can you tell me how I can fix these or what I'm doing wrong
     
  7. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You need to put the functions and variables inside a class.
     
  8. vipergabe

    vipergabe

    Joined:
    Jan 28, 2021
    Posts:
    5
    If this is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public KeyCode[] validSequenceKeys = new[] {
    KeyCode.UpArrow,
    KeyCode.RightArrow,
    KeyCode.DownArrow,
    KeyCode.LeftArrow
    };
    KeyCode[] GenerateSequence(int length = 6) {
    KeyCode[] sequence = new KeyCode[length];
    for (int i = 0; i < length; i++) {
    var key = validSequenceKeys[Random.Range(0, validSequenceKeys.Length)];
    sequence = key;
    }
    return sequence;
    }

    What is it exactly that I need to add or change, because I tried changing "public" to "public class" but that just gave me 15 me errors, I'm pretty bad at this if no one can already tell.
     
  9. Uthgaard

    Uthgaard

    Joined:
    Dec 17, 2020
    Posts:
    25
    What he's saying is you nuked part of the necessary code when you pasted that in. Open up a new script and start over, taking care not to paste over top of anything that's already there.
     
  10. vipergabe

    vipergabe

    Joined:
    Jan 28, 2021
    Posts:
    5
    that still gives the same error, could it be anything else?
     
  11. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    @vipergabe You will need to broadly familiarize yourself with C# syntax before you can do anything but copy other people's code (and even then you will run into problems, as you are experiencing). Unlike human languages, it matters if you botch the grammar even a little bit on computer languages. Computers need your code to be exactly right for it to work -- otherwise it will break. Check out something like this, which will give you a basic introduction into how C# syntax works in the context of Unity: https://www.youtube.com/watch/nWkUutm7Kus There's also a unity wiki page with more information here: http://wiki.unity3d.com/index.php?title=The_Absolute_Beginner's_Guide_to_Unity

    Additionally, keep searching for information on how to write C# outside the context of Unity. Microsoft has a lot of documentation on this topic: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/
     
  12. Uthgaard

    Uthgaard

    Joined:
    Dec 17, 2020
    Posts:
    25
    Every script starts with

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.    
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.    
    17.     }
    18. }
    Your code is missing basically all of that. In general, variables go after public class {, before void start.
    Operational code goes inside start or updates' brackets, or maybe a different method that isn't normally there by default.
    At this stage you should not replace any of it.

    I'd strongly strongly strongly recommend following the Unity Learn essentials & beginner pathways. It has you work on your own project as you learn the basics.

    https://learn.unity.com/pathway/unity-essentials

    Then

    https://learn.unity.com/pathway/junior-programmer
     
    vipergabe likes this.