Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Giaddon · Apr 13, 2014 at 09:23 PM · variablesstrings

How to update variables in a string when displayed?

Hello everyone,

I have a lot of strings I want to use for character dialogue. Right now I store them in a dialogue.js as public variables and grab them when needed (public var greeting : String = "Hello, mate!";) and then I update the GUI with the needed string. But there's a big problem with this method: any variables in the strings themselves (the player name, or number of items the player is carrying, etc.) remain at the same value as when the game started.

So how can I format this so it doesn't bake the variable into the string until the dialogue is actually put on screen?

Thanks!

Comment
Add comment · Show 3
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 getyour411 · Apr 13, 2014 at 09:52 PM 0
Share

Show an example of the code for player # of items and where you are getting that - once in Awake()/Start(), via a GetComponent reference, etc.

avatar image Giaddon · Apr 13, 2014 at 10:15 PM 0
Share

Something like this:

Dialogue.js

 var character: CharacterScript; //linked to character through inspector
 
 var Greeting : String = "Hello, mate!";
 var youHave : String = "You have " + character.coconuts + " coconuts!";

CharacterScript.js

     var name: String = "Bradley";
     var coconuts: int = 0;

GUI.js

 GUI.Box (Rect (80,60,874,648), activeText);

Talking.js

function Update () {

         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
             GUIScript.activeText = Dialogue.youHave;
             }
         }
 
avatar image Giaddon · Apr 15, 2014 at 07:11 PM 0
Share

Consider this solved! I used an adapted version of akauper's solution. I created a function that reassigns the string variables when the player starts dialogue. Thanks, everyone!

1 Reply

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

Answer by akauper · Apr 14, 2014 at 01:28 AM

 //Dialogue.js
 var character : CharacterScript;
 var greeting : String = "Hello, mate!";

 //CharacterScript.js
 var name : String = "Bradley";
 var coconuts : int = 0;

 //GUI.js
 var activeText : String = "";
 GUI.Box(Rect (80,60,874,648), activeText);

 //Talking.js
 var dialogue : Dialogue; //set a reference here however u want(inspector is fine)
 var character : CharacterScript; //set a reference here as well.
 function Update()
 {
     if(Input.GetKyeDown(KeyCode.Space))
     {
         GUIScript.activeText = dialogue.greeting + " You have " + character.coconuts + " coconuts!";
     }
 }
Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accesing variable from another class for instantiated gameobjects 0 Answers

Using string to refer to variable 2 Answers

check if my variable is in the same position as the array 2 Answers

Alter variables in other Game Objects 1 Answer

Get Component With Variable Help 2 Answers


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