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

Problem with Text-based choose your own adventure game

Discussion in 'Scripting' started by luxetc, Apr 19, 2019.

  1. luxetc

    luxetc

    Joined:
    Apr 17, 2019
    Posts:
    4
    Hey all.

    I've started trying to learn basic C# and unity as of last week, I've been spending hours daily watching tutorials, aswell as watching a course I signed up to and just trying to figure stuff out on my own..
    With all that being said, this stuff is extremely overwhelming, I don't want to give up but the more I try to implement my ideas into code the more complicated and confusing it gets.. I guess that's how it is for everyone though.
    I decided to take it down a notch and focus on getting to know C# better and to make a somewhat simple text adventure game.. which turns out to be equally as hard for me.

    Instead of moving through scenes to continue the story, I'm trying to just change states, where the text will change for both the story and button options. My current problem is not being able to move past state_1 and state_2 methods, like it doesn't even register on the EventSystem.. it seems to me as if I need to reset the button in order for it to choose another state or something.
    Or even a better alternative would be to have some sort of a database script to hold all these states and have the buttons call different states depending on the one that is currently running.. No idea where to even begin on that.

    Here's the visual:


    here's the dumb script I wrote:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6. using UnityEngine;
    7.  
    8. public class NewTextManager : MonoBehaviour
    9. {
    10.  
    11.     public Text theMainText;
    12.     public Text buttonText1;
    13.     public Text buttonText2;
    14.     public Button Button1;
    15.     public Button Button2;
    16.  
    17.     public bool selection1;
    18.     public bool selection2;
    19.  
    20.     public enum States { mainstate, state_1, state_2, state_3, state_4 }
    21.     private States theState;
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         theState = States.mainstate;
    27.     }
    28.     void OnEnable()
    29.     {
    30.         Button1.onClick.AddListener(delegate { selection1 = true; });
    31.         Button2.onClick.AddListener(delegate { selection2 = true; });
    32.  
    33.     }
    34.     void Update()
    35.     {
    36.  
    37.         if (theState == States.mainstate)
    38.         {
    39.             mainstate();
    40.         }
    41.         if (theState == States.state_1)
    42.         {
    43.             state_1();
    44.         }
    45.         if (theState == States.state_2)
    46.         {
    47.             state_2();
    48.         }
    49.         if (theState == States.state_3)
    50.         {
    51.             state_3();
    52.         }
    53.         if (theState == States.state_4)
    54.         {
    55.             state_4();
    56.         }
    57.     }
    58.  
    59.  
    60.     void mainstate()
    61.     {
    62.         theMainText.text = "something happens";
    63.         buttonText1.text = "do something";
    64.         buttonText2.text = "do another thing";
    65.  
    66.         if (selection1 == true)
    67.         {
    68.             theState = States.state_1;
    69.         }
    70.         else if (selection2 == true)
    71.         {
    72.             theState = States.state_2;
    73.         }
    74.     }
    75.     void state_1()
    76.     {
    77.         EventSystem.current.SetSelectedGameObject(null); //the only reason i use these is to deselect the button so it won't be highlighted anymore after being clicked
    78.         theMainText.text = "something happens again";
    79.         buttonText1.text = "option1";
    80.         buttonText2.text = "option2";
    81.  
    82.         if (selection1 == true)
    83.         {
    84.             theState = States.state_3;
    85.         }
    86.         else if (selection2 == true)
    87.         {
    88.             theState = States.state_4;
    89.         }
    90.     }
    91.     void state_2()
    92.     {
    93.         EventSystem.current.SetSelectedGameObject(null);
    94.         theMainText.text = "something happens again";
    95.         buttonText1.text = "option1";
    96.         buttonText2.text = "option2";
    97.  
    98.         if (selection1 == true)
    99.         {
    100.             theState = States.state_3;
    101.         }
    102.         else if (selection2 == true)
    103.         {
    104.             theState = States.state_4;
    105.         }
    106.     }
    107.     void state_3()
    108.     {
    109.         EventSystem.current.SetSelectedGameObject(null);
    110.         theMainText.text = "something really bad happens";
    111.         buttonText1.text = "option1";
    112.         buttonText2.text = "option2";
    113.     }
    114.     void state_4()
    115.     {
    116.         EventSystem.current.SetSelectedGameObject(null);
    117.         theMainText.text = "something really good happens";
    118.         buttonText1.text = "option1";
    119.         buttonText2.text = "option2";
    120.     }
    121. }
    122.  
    Your help will be immensely appreciated. Thank you!
     
    Last edited: Apr 19, 2019
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Welcome. You'll get more support here if you format your code with code tags. Good luck!
     
  3. luxetc

    luxetc

    Joined:
    Apr 17, 2019
    Posts:
    4
    You're right I completely forgot about it. Edited it! Thanks
     
  4. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    Though it's nice to see that you are using delegates, which can be really helpful, I don't think you need them here. I think the main problem with your approach is that it is not very dynamic. Have you watched the official tutorial here on making a simple quiz game? I think this would be a good starting point on how to structure your code.
    Even better if you use scriptable objects, I think I'll make a tutorial on that soon since I just wrote a text adventure for a game jam.
     
  5. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    First and most importantly, welcome, and don't give up. Secondly, BambooHutGames is right, this is most likely (read: definitely) not the right approach. I second what Bamboo says on watching the official tutorials, they're excellent. Thirdly, I suspect the reason your code specifically isn't working is likely to do with the fact that you're never actually setting selection1 and selection2 back to false. Good luck, and keep at it.
     
    Joe-Censored likes this.
  6. luxetc

    luxetc

    Joined:
    Apr 17, 2019
    Posts:
    4
    I took a look at it earlier and didn't really understand the need for scriptable objects in my scenario but i'll watch the whole thing just to see if I didn't miss anything. Thank you for the suggestion!!

    That makes complete sense lol. But yes, as you and BambooHut mentioned I should defo take a different path with the script. Thank you both for your input
     
  7. Deleted User

    Deleted User

    Guest

  8. luxetc

    luxetc

    Joined:
    Apr 17, 2019
    Posts:
    4
  9. Deleted User

    Deleted User

    Guest

    I just wanted to point out that you didn't need Unity to make a text based game. ;)