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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Button Click Game

Discussion in 'Scripting' started by unity_9TRiDWnO7W2QuQ, Sep 13, 2018.

  1. unity_9TRiDWnO7W2QuQ

    unity_9TRiDWnO7W2QuQ

    Joined:
    Apr 16, 2018
    Posts:
    3
    Hey so I want to create a little button click game where you have 2 buttons and you have to click the right one to proceed to the next level.

    So at level 1 you need to click one right level 2 two and so one.

    The sequence should be the same for every level but I have no idea how to get it working...

    Any conclussions or ideas for that topic?

    Greetings
     
  2. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Something like this:

    Code (CSharp):
    1. public class Level : MonoBehavior
    2. {
    3.      int currentLevel = 1;
    4.      int clicksOnCurrentLevel = 0;
    5.  
    6.      // This is the code that runs when you click the button.
    7.      {
    8.           // Increment the number of clicks you've made by one
    9.           clicksOnCurrentLevel++;
    10.          
    11.           // Check if you've got enough clicks to level up and, if yes, do so
    12.           if (clicksOnCurrentLevel >= ClicksRequiredToCompleteLevel())
    13.           {
    14.                LevelUp();
    15.           }
    16.      }
    17.  
    18.      // This is where we calculate on the fly how many clicks are needed to level up
    19.      private int ClicksRequiredToCompleteLevel()
    20.      {
    21.           return currentLevel * 2;
    22.      }
    23.  
    24.      // This is the code we use to level up - it increments the level and resets the number of clicks
    25.      private void LevelUp()
    26.      {
    27.              currentLevel++;
    28.              clicksOnCurrentLevel = 0;
    29.      }
    30. }
    Add this code to any gameObject in the Scene, create a button, and, in its OnClick functionality (check Unity's official tutorials), refer to the Click() method - meaning, it'll be the method that gets called whenever you click the button.

    By the way, I got started with Unity by developing incremental games, and honestly think it's the best way to learn the engine - so, good luck!
     
  3. unity_9TRiDWnO7W2QuQ

    unity_9TRiDWnO7W2QuQ

    Joined:
    Apr 16, 2018
    Posts:
    3
    Hey thamks for the fast response!

    I meant it more like you need to do a sequence to come to the next level.

    For example at level 5 its : Button1,Button1,Button1,Button2,Button1
     
  4. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Untested, but this should get you on the right track:

    Code (CSharp):
    1. // Attach this script to each button
    2. public class Button : MonoBehavior
    3. {
    4.  
    5.     // Set this in the inspector
    6.     public int buttonID;
    7.  
    8.     GameController gameController;
    9.  
    10.  
    11.     private void Start()
    12.     {
    13.         // Get reference to the GameController singleton
    14.         gameController = FindObjectOfType<GameController>();
    15.     }
    16.  
    17.     // Call this when the button is clicked - it passes the ID of the pressed button to the GameController
    18.     public void Click()
    19.     {
    20.         gameController.RegisterButtonClick(buttonID);
    21.     }
    22.  
    23. }
    24.  
    Code (CSharp):
    1. // This is the GameController singleton that will handle button clicks - make sure you don't have more than one of those in the scene.
    2. public class GameController: MonoBehavior
    3. {
    4.  
    5.     int currentLevel = 0;
    6.     int clicksOnCurrentLevel = 0;
    7.  
    8.     List<Sequence> sequences = new List<Sequence>();
    9.  
    10.     private void Awake()
    11.     {
    12.         // Create the correct sequence for Level 0 - and do it for as many levels as you want
    13.         Sequence level0 = new Sequence();
    14.         level0.correctSequence.Add(1);
    15.         level0.correctSequence.Add(3);
    16.         level0.correctSequence.Add(4);
    17.         sequences.Add(level0);
    18.     }
    19.  
    20.     public void RegisterButtonClick(int buttonID)
    21.     {
    22.         if (isCorrectButton(buttonID))
    23.         {
    24.             clicksOnCurrentLevel++;
    25.             Debug.Log("Correct button pressed!");
    26.      
    27.             // Check if you've got enough clicks to level up and, if yes, do so
    28.             if (clicksOnCurrentLevel >= ClicksRequiredToCompleteLevel())
    29.             {
    30.                LevelUp();
    31.             }
    32.         }
    33.  
    34.         else
    35.         {
    36.             Debug.Log("Incorrect button pressed!");
    37.         }
    38.      
    39.  
    40.     }
    41.  
    42.     private bool isCorrectButton(int buttonID)
    43.     {
    44.         // This is the big one.
    45.         // 1. First, we get the ID of the button that was clicked
    46.         // 2. Next, we get the level-appropriate sequence from the array of sequences we created earlier
    47.         // 3. Then, we check whether the ID of the button that was clicked corresponds to the number (aka, ID) that should be clicked given that particular number of clicks made on the current level
    48.         // 4. If it does, we return it as a valid click
    49.         if ((buttonID) == sequences[currentLevel].correctSequence[clicksOnCurrentLevel]){ return true;}
    50.         else {return false;}
    51.  
    52.     }
    53.  
    54.      // This is where we calculate on the fly how many clicks are needed to level up
    55.     private int ClicksRequiredToCompleteLevel()
    56.     {
    57.         return currentLevel + 1;
    58.     }
    59.  
    60.     // This is the code we use to level up - it increments the level and resets the number of clicks
    61.     private void LevelUp()
    62.     {
    63.         currentLevel++;
    64.         clicksOnCurrentLevel = 0;
    65.     }
    66.  
    67. }
    Code (CSharp):
    1. // We use a struct to store each level's sequence
    2. public struct Sequence
    3. {
    4.     // This is the list of integers containing the correct sequence of buttonIDs to be pressed for that specific level
    5.     public List<int> correctSequence = new List<>();
    6. }