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

Randomly select a page state

Discussion in 'Scripting' started by Fuzzytron, Mar 20, 2018.

  1. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    Hi guys,

    I am implementing a question system into my game where if a user enters a trigger they are given a question to answer. This trigger works however it only displays the one question I tell it to. I am aiming to have say 5 questions that I want to randomly be displayed to the user, so they could receive any of the 5 questions when they enter the trigger.

    I thought I could do this by somehow randomly calling the appropriate page state for that question. Here is my method for when the player is questioned (calls when they enter the trigger) and my page states:

    Code (CSharp):
    1. void PlayerQuestioned()
    2.     {
    3.         var questions = [SetPageState(PageState.Question), SetPageState(PageState.Question2),
    4.             SetPageState(PageState.Question3), SetPageState(PageState.Question4), SetPageState(PageState.Question5)];
    5.  
    6.         questions[Random.Range(0, questions.Length)]();
    7.     }
    8.  
    9.     void SetPageState(PageState state)
    10.     {
    11.         switch (state)
    12.         {
    13.             case PageState.None:
    14.                 MainMenuPage.SetActive(false);
    15.                 GameOverPage.SetActive(false);
    16.                 ScoreboardPage.SetActive(false);
    17.                 AchievementsPage.SetActive(false);
    18.                 TimerPage.SetActive(false);
    19.                 QuestionPage.SetActive(false);
    20.                 break;
    21.  
    22.             case PageState.MainMenu:
    23.                 MainMenuPage.SetActive(true);
    24.                 GameOverPage.SetActive(false);
    25.                 ScoreboardPage.SetActive(false);
    26.                 AchievementsPage.SetActive(false);
    27.                 TimerPage.SetActive(false);
    28.                 QuestionPage.SetActive(false);
    29.                 break;
    30.  
    31.             case PageState.GameOver:
    32.                 MainMenuPage.SetActive(false);
    33.                 GameOverPage.SetActive(true);
    34.                 ScoreboardPage.SetActive(false);
    35.                 AchievementsPage.SetActive(false);
    36.                 TimerPage.SetActive(false);
    37.                 QuestionPage.SetActive(false);
    38.                 break;
    39.  
    40.             case PageState.Scoreboard:
    41.                 MainMenuPage.SetActive(false);
    42.                 GameOverPage.SetActive(false);
    43.                 ScoreboardPage.SetActive(true);
    44.                 AchievementsPage.SetActive(false);
    45.                 TimerPage.SetActive(false);
    46.                 QuestionPage.SetActive(false);
    47.                 break;
    48.  
    49.             case PageState.Achievements:
    50.                 MainMenuPage.SetActive(false);
    51.                 GameOverPage.SetActive(false);
    52.                 ScoreboardPage.SetActive(false);
    53.                 AchievementsPage.SetActive(true);
    54.                 TimerPage.SetActive(false);
    55.                 QuestionPage.SetActive(false);
    56.                 break;
    57.  
    58.             case PageState.Timer:
    59.                 MainMenuPage.SetActive(false);
    60.                 GameOverPage.SetActive(false);
    61.                 ScoreboardPage.SetActive(false);
    62.                 AchievementsPage.SetActive(false);
    63.                 TimerPage.SetActive(true);
    64.                 QuestionPage.SetActive(false);
    65.                 break;
    66.  
    67.             case PageState.Question1:
    68.                 MainMenuPage.SetActive(false);
    69.                 GameOverPage.SetActive(false);
    70.                 ScoreboardPage.SetActive(false);
    71.                 AchievementsPage.SetActive(false);
    72.                 TimerPage.SetActive(false);
    73.                 QuestionPage.SetActive(true);
    74.                 break;
    75.         }
    76.     }
    Currently I only have Question1 in my page states, but I hope you get my idea. I do not want to randomly select any of the other page states, simply the page states I will call Question1, Question 2, Question3, Question 4 and Question5.

    Have I gone about this the right way? I am a novice. The [ bracket is currently marked as an invalid expression term when surrounding the page states in the PlayerQuestioned() method.

    Any advice would be appreciated.
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think this is what you where trying to do.
    Take 5 states, pick from them randomly. and then turn a bunch of stuff on or off

    Code (CSharp):
    1. void PlayerQuestioned()
    2.     {
    3.         PageState[] questions =  new PageState[] {PageState.Question, PageState.Question2, PageState.Question3, PageState.Question4, PageState.Question5};
    4.        
    5.         SetPageState(questions[Random.Range(0, questions.Length-1)]);
    6.     }
     
    Fuzzytron likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I think that Random.Range will be the int version, so you don't want to -1 as the high value is exclusive in the int version.

    So Range(0, 5) would return a value of 0-4 for example.
     
    Fuzzytron likes this.
  4. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    I think you're right as I've just done some testing and question 5 is never showing up, but question 1 - 4 are showing up. Do I just remove -1 then or does it require just a 0?
     
  5. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    That worked perfectly, except the last question is not showing up so I think what Brathnann is adding to your point may be right too!
     
  6. Fuzzytron

    Fuzzytron

    Joined:
    Feb 22, 2018
    Posts:
    13
    Never mind i just removed the -1 entirely, now it works perfectly! Thanks both of you!
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Yep, that's all you had to do. Random.Range with floats is inclusive, thus the top number is included. But if you use ints, it's exclusive. It's a common mix-up since the behavior is different.
     
    Fuzzytron likes this.
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    cool, i'm glad you guys worked it out. I don't always remember why I put that -1 in the random.range. It's a bad habit that bites me in the butt.
     
    Fuzzytron likes this.