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

Help with basic structure behind unique quiz game?

Discussion in 'Scripting' started by TheNomadicAspie, Mar 12, 2021.

  1. TheNomadicAspie

    TheNomadicAspie

    Joined:
    Mar 11, 2021
    Posts:
    26
    I last used Unity a few years ago and am not very good. I've spent all day thinking about how to structure my project before beginning, and was hoping someone could help.

    Basically I'm creating a very simple quiz game with 9 buttons. But there are no timers, no penalties for getting the questions wrong. The only mechanic is that when the correct button is pressed, an image appears. After the correct button is pressed, a new button becomes the "correct" one, and a new image appears when that button is pressed.

    I know there are several ways to do this, but I'm trying to figure out how to accomplish this in an easily scalable way to avoid repetitive code. There will be several levels and no random questions.

    So for example in level 1, a different image will be displayed after pressing button 1, then 4, then 5. In level 2, a different image will be displayed after pressing button 3, then 8, then 6.

    The problem I'm running into is do I create a different script for each button and have the update method constantly check if a button is pressed, then have it check another script's variables that constantly updates what the "correct" answer button is and compares the value of the button with the "correct" answer?

    Or do I create a different script for each "level" with the sprites to be displayed as private GameObjects, then have that script keep checking if the correct button is pressed down before displaying the sprite?

    I've watched tutorials for 10+ "quiz games" in Unity, but haven't been able to make any progress since my main issue is figuring out the structure behind how I should build this. It's going to take me awhile to figure out what I'm doing, but I don't want to start coding until I can figure out the layout for the project. But since I'm relatively new to Unity, I don't know the best way to do it and have just been staring at my computer all day accomplishing nothing.

    Any tips?
     
  2. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    Hey, have you tried a custom class or scriptableobject for each question or answer?
     
  3. TheNomadicAspie

    TheNomadicAspie

    Joined:
    Mar 11, 2021
    Posts:
    26
    I have not, but that's a really good idea. Thank you!

    I do still need to figure which objects should have scripts and how they should interact with each other, but having the questions as a class is a really good idea so thanks a lot.
     
  4. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    No problem, just came into my mind when I saw this problem! Just message me if you need anything.
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    Your game code shouldn't care about the idea of buttons at all. The buttons are a separate visual that will react to changes in the game controller/manager.
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Some more details would be helpful, like
    "So for example in level 1, a different image will be displayed after pressing button 1, then 4, then 5. In level 2, a different image will be displayed after pressing button 3, then 8, then 6."
    is unclear: is it button 5 in level 1 that triggers the image, or 1-4-5?

    Regardless, I would have an overall manager that tracks everything. The button script would be very simple (and is reusable for all 9), it just updates appearance if needed and sends the main script its index, which would be a public int you set in the Inspector). The manager does all the logic.
     
  7. TheNomadicAspie

    TheNomadicAspie

    Joined:
    Mar 11, 2021
    Posts:
    26
    Ok thanks, that's obvious now that you say it. I'm so used to seeing people have a bunch of scripts for every object in the tutorials I've watched, I forgot I can just declare the buttons in the code and drag them in to a main script from the inspector.
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    That would give you full access to their scripts, so is more robust than my original thought (which was for "upstream" or button-to-main-script communication only). You'll likely need at least a few commands going downstream, maybe a reset for a new level, etc. So I'd do it your way. Use an array so you can easily loop through them:

    public Button[] myButtonArray = new Button[9];
     
  9. TheNomadicAspie

    TheNomadicAspie

    Joined:
    Mar 11, 2021
    Posts:
    26
    Yeah I think I know how to do it now. Thanks again for the help.
     
    seejayjames likes this.