Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Vento313 · Mar 02, 2016 at 07:35 PM · c#buttontextscenegame

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: alt text

It is going to have a lot of choices and branching stories depending on the choices you make. Something like this: alt text

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.

game-screen.png (10.2 kB)
gameexample.png (22.9 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Mar 02, 2016 at 10:59 PM 3
Share

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.

avatar image Vento313 Le-Pampelmuse · Mar 03, 2016 at 08:42 PM 0
Share

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.

avatar image Le-Pampelmuse Vento313 · Mar 06, 2016 at 03:21 AM 3
Share

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 ;)

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vento313 · Mar 03, 2016 at 08:39 PM 0
Share

So i need to make different scripts for every node? I'm going to end up with lots of scripts if I do that.

avatar image Jessespike Vento313 · Mar 07, 2016 at 06:34 PM 1
Share

What? No. You just need these 2 scripts.

avatar image Le-Pampelmuse Jessespike · Mar 07, 2016 at 08:03 PM 3
Share

When providing whole scripts to beginners it would make sense to explain them properly. Either by //comments or a step by step guide. ;)

avatar image Vento313 · Mar 07, 2016 at 09:30 PM 0
Share

After some testing I managed to make a version of the game that works with a similar script. Thanks for the input!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges