Text adventure game, how to change text in a specific way with C#
Hello! I'm currently trying to develop a "Choose your own adventure" text game. This is a screenshot of the game:
It is going to have a lot of choices and branching stories depending on the choices you make. Something like this:
Is it possible to develop a game like this using only 1 scene? I tried in many different ways but can't seem to get it to work.
Hi.
This isn't overly complicated to achieve. I did kind of exactly the same for android once to experiment with android-oriented coding. For an absolute beginner however it can be a challenge.
"I tried in many different ways but can't seem to get it to work."
Well, what have you tried? Let's start with that.
What doesn't "work" like you want it to?
Would be a good information to give away if you want help. ;)
I suggest you take a good look at the UI tutorials as well as the beginner scripting tutorials. You can find them in the Learn Section.
To answer your initital question: "how to change text in a specific way with C#":
Text.text, This is asked and answered multiple times already and clearly described in tutorials and documentation.
To answer your second question, "Can it be achived in one single scene":
Yes it can. Theoretically there is no need to switch scenes, but it is much more efficient (especially for 3D games) to just discard the finished scene with it's assets and loading a new scene, rather than instantiating and destroying everything in one huge scene.
For simple and short games like your's or presentations, it is not neccessary to use multiple scenes.
Thanks for your suggestion! I'm currently following a Unity course, I just wanted to see if I could make a $$anonymous$$i-game by myself, but I guess I'm not ready :)
As for what I tried, I tought about having an int that changed value based on what button you click, for example: I click on the first button and the int goes to 1, if I click on the second button it goes to -1, then an if statement checked the int's value and changed the text based on that. The choices in the game are going to be a lot so you can see that doesn't work.
I would recommend you use your UI canvas like you already have. You can keep it over the entire game. Read the documentation and scripting reference about buttons and everything else if you are unsure about a specific Unity component.
You will start with the main script, that holds all references you need, to the Question Text for example. It also keeps track of which level in your decision-tree you are currently in. Depending on the level and choice the player made you would then change the text of the question's text to the respective string in your database. I recommend having a seperate script with all your possible texts as a public static string
in it, so you can quickly access them and change the question in your game manager script.
You could write a function that takes a boolean type for the player choices "yes" or "no". Then have the buttons execute that function in their onclick event and the no button feeds the function a false boolean and the yes button a true boolean.
What happens then is up to your wishes, I highly recommend you finish the Unity course, and watch the beginner scripting tutorials as I said earlier.
PS: This is starting to go beyond what Unity Answers is for. It's not for discussion and arguing about methods. There are many forums containing tutorials and discussion threads.
Your initial question is answered I guess. I hope your learning progress goes s$$anonymous$$dily upwards ;)
Answer by Jessespike · Mar 02, 2016 at 07:58 PM
Yeah, it's possible. I would create two scripts: One to store references of the UI components and another script that would act as a story node. The node would contain the text for the message and buttons. Clicking on a button would update the text on the UI components. Nodes would be linked to other nodes.
Example:
using UnityEngine.UI;
public class TextAdventureGui : MonoBehaviour {
public Text m_text;
public Button m_button1;
public Button m_button2;
public TextAdventureNode firstNode;
public void Start()
{
ShowNode(firstNode);
}
void ShowNode(TextAdventureNode node)
{
m_text.text = node.m_text;
m_button1.GetComponentInChildren<Text>().text = node.m_button1Text;
m_button2.GetComponentInChildren<Text>().text = node.m_button2Text;
m_button1.onClick.RemoveAllListeners();
m_button2.onClick.RemoveAllListeners();
m_button1.onClick.AddListener(()=>ShowNode(node.m_nextNode1));
m_button2.onClick.AddListener(()=>ShowNode(node.m_nextNode2));
}
}
.
using UnityEngine.UI;
public class TextAdventureNode : MonoBehaviour {
public string m_text;
public string m_button1Text;
public string m_button2Text;
public TextAdventureNode m_nextNode1;
public TextAdventureNode m_nextNode2;
}
So i need to make different scripts for every node? I'm going to end up with lots of scripts if I do that.
When providing whole scripts to beginners it would make sense to explain them properly. Either by //comments or a step by step guide. ;)
After some testing I managed to make a version of the game that works with a similar script. Thanks for the input!
Your answer
Follow this Question
Related Questions
how can i make the name of a clicked button appears as text in another scene 1 Answer
How do I make a credits button? 1 Answer
How to implement voice overs linked with buttons? 0 Answers
How To pass from level to the next after winning, with one button ? 1 Answer
Making text appear by pressing a button, only when player is close to the object 1 Answer