Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A simple RSP game using Unity Engine UI (w/ scoring function) [for 2020.3]

Discussion in 'Game Design' started by Stobros1, Jul 3, 2021.

  1. Stobros1

    Stobros1

    Joined:
    Jun 15, 2021
    Posts:
    13
    So, I was thinking about making a rock-scissors-paper game using a C# script and UI game objects instead of other ways I found which is good, but it's already outdated, so to speak. The other functions I want in the game is to have a scoring function on-screen, and the code should also be versatile enough to add other auxiliary elements to fully customize the game to suit anyone's tastes.

    Anybody interested of helping me out? Thanks in advance.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    What specific help to you feel you need? This sounds like the kind of project that you might find in a beginner tutorial. In fact, searching for "Unity rock paper scissor tutorial" shows several full tutorials on making a rock paper scissors game, start to finish. What help do you need that isn't already covered in one of those tutorials?
     
    Not_Sure and Stobros1 like this.
  3. Stobros1

    Stobros1

    Joined:
    Jun 15, 2021
    Posts:
    13
    Well, I found one but it doesn't use UI elements to display all the action.
     
  4. Stobros1

    Stobros1

    Joined:
    Jun 15, 2021
    Posts:
    13
    OK, as it turns out, all the C# scripts I found elsewhere, does not work for Unity at least on how I understand the scripts. Is there a way to convert this to a valid compatible code?

    Code (CSharp):
    1. static void Main(string[] args)
    2. {
    3.  
    4.     string inputPlayer, Computer;
    5.     int randomnum;
    6.     string loop;
    7.     bool keepPlaying = true;
    8.     string[] validMoves = new string[3] { "R", "P", "S" };
    9.     int wins = 0;
    10.     int Loses = 0;
    11.     int ties = 0;
    12.     while (keepPlaying)
    13.     {
    14.       // while (keepPlaying) // You can get rid of this while loop as it is not helping you out.
    15.       // { // second while loop opening
    16.             Random myRandomObject = new Random();
    17.             randomnum = myRandomObject.Next(1, 4);
    18.             Console.Write("To play: enter R for Rock, S for Scissors, P for Paper.");
    19.             inputPlayer = Console.ReadLine();
    20.             inputPlayer = inputPlayer.ToUpper();
    21.  
    22.             if (!validMoves.Contains(inputPlayer))
    23.             {
    24.                 Console.WriteLine("Please select a valid move.");
    25.                 continue;
    26.             }
    27.  
    28.             switch (randomnum)
    29.             {
    30.                 case 1:
    31.                     Computer = "Rock";
    32.                     Console.WriteLine("The computer played Rock");
    33.                     if (inputPlayer == "R")
    34.                     {
    35.                         Console.WriteLine("Tie!!\n\n");
    36.                         ties++;
    37.                     }
    38.                     else if (inputPlayer == "P")
    39.                     {
    40.                         Console.WriteLine("You win!!\n\n");
    41.                         wins++;
    42.                     }
    43.                     else if (inputPlayer == "S")
    44.                     {
    45.                         Console.WriteLine("Computer wins!!\n\n");
    46.                         Loses++;
    47.                     }
    48.                     break;
    49.                 case 2:
    50.                     Computer = "Paper";
    51.                     Console.WriteLine("The computer played Paper");
    52.                     if (inputPlayer == "P")
    53.                     {
    54.                         Console.WriteLine("Tie!!\n\n");
    55.                         ties++;
    56.                     }
    57.                     else if (inputPlayer == "R")
    58.                     {
    59.                         Console.WriteLine("Computer wins!!\n\n");
    60.                         Loses++;
    61.                     }
    62.                     else if (inputPlayer == "S")
    63.                     {
    64.                         Console.WriteLine("You win!!\n\n");
    65.                         wins++;
    66.                     }
    67.                     break;
    68.                 case 3:
    69.                     Computer = "Scissors";
    70.                     Console.WriteLine("The computer played Scissors");
    71.                     if (inputPlayer == "S")
    72.                     {
    73.                         Console.WriteLine("Tie!!\n\n");
    74.                         ties++;
    75.                     }
    76.                     else if (inputPlayer == "R")
    77.                     {
    78.                         Console.WriteLine("You win!!\n\n");
    79.                         wins++;
    80.                     }
    81.                     else if (inputPlayer == "P")
    82.                     {
    83.                         Console.WriteLine("Computer wins!!\n\n");
    84.                         Loses++;
    85.                     }
    86.                     break;
    87.                 default: // You can get rid of this default block, it won't ever hit.
    88.                     Console.WriteLine("Please enter a correct entry");
    89.                     break;
    90.             }
    91.  
    92.             Console.WriteLine("Scores:\tWins:\t{0},\tLoses:\t{1},\tties:\t{2}", wins, Loses, ties);
    93.  
    94.             Console.WriteLine("Would you like to continue playing? (y/n)");
    95.             loop = Console.ReadLine();
    96.            
    97.             if (loop == "y")
    98.             {
    99.                 keepPlaying = true;
    100.             }
    101.             else if (loop == "n")
    102.             {
    103.                 keepPlaying = false;
    104.             }
    105.         // } // second while loop closing
    106.  
    107.     }
    108. }
    What also disappoints me is that I cannot even use Random in this particular condition.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    If you're going to find random C# code and try to is it in your game, you shouldn't expect it to "just work". You should expect to have to read through and understand the code, and understand how you'd need to modify it to make it work within Unity. That requires a bit of existing general knowledge about C# and Unity, which you'll hopefully get by doing some tutorials.

    As for the specific code you've included, that appears to be a basic .NET console program, which runs just fine if you just tried to create a Console application in Visual Studio. But you're not going to use
    static void Main
    in a Unity script. And all of the use of "Console." would need some other solution within Unity. The use of "Random" could work fine in Unity. You just need to fully qualify it, or include the correct
    using
    statement (so that it's using System.Random rather than UnityEngine.Random).

    Basically, to use this bit of code in Unity, it would take a few small tweaks. If you don't feel comfortable figuring out how to make those tweaks, I really recommend doing some more tutorials first and improving your C# knowledge. You're not going to get too far copy/pasting stuff without understanding how it works, especially if it needs some adjustments.
     
    Stobros1 and Ryiah like this.
  6. Stobros1

    Stobros1

    Joined:
    Jun 15, 2021
    Posts:
    13
    Wow, thanks for the advice!