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 KeithRichter · Feb 13, 2014 at 04:02 PM · variables

Changing a variable to the next one in order automatically..

Currently I'm using GUI.Label and I am using a variable in the string section to tell it what string to display. There is a lot of different text to go to and from and I am trying to figure out how to tell that variable to change to a new string.

The basic setup:

 string slide1 = "Slide 1 test";
     string slide2 = "Slide 2 test";
     void OnGUI() 
     {
 
         GUI.Label(new Rect(50, Screen.height-130, Screen.width-100, 100), slide1, "TextField");
     }
 
 
     void Update(){
         if(Input.GetMouseButtonDown(0))
         {
             ??????????????????????????
         }
     }

I could manually tell it after mouse down to slide1 = slide2; or something but then what do I do for slide3 and 4 and so on and so on? I could just tell it forever and ever to keep redoing the thing manually (if on slide1 go to slide2 if on slide2 go to slide3 etc.) but that seems like it'd be really inefficient and I'm missing some fundamental programming skill I should have to resolve this.

Comment
Add comment
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

1 Reply

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

Answer by perchik · Feb 13, 2014 at 04:07 PM

I think you need to create a list. If you put all your strings in a list and then keep an integer that points to which place you are in the list. Something like this: (fair warning, this code probably doesn't work, but should give you the right idea)

 List<string> myStrings = new List<string>();
 myStrings.Add("Message1");
 myStrings.Add("Message2");
 
 int indx= 0;
 
 void Update(){
    if(Input.GetMouseButtonUp(0)){
       indx = indx +1 % myStrings.Length(); 
       //Add one to the index, but make it wrap around
    }
 } 
 
 void OnGui(){
     GUI.Label(new Rect(50, Screen.height-130, Screen.width-100, 100), myStrings[indx],"TextField");
 
 }

Comment
Add comment · Show 1 · 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 Hoeloe · Feb 13, 2014 at 08:35 PM 0
Share

Eww. If you're not adding or removing elements to it, do not use a list. They have more verbose syntax and are slightly slower than their fixed-length alternative, arrays. Use arrays wherever possible - they are incredibly fast. If you don't need to add or remove elements at runtime, then don't use lists.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Click sphere for variables 0 Answers

Use a String Variable to Dynamically Invoke a Method 1 Answer

Difficulty Calling Variables from other scrtipts 1 Answer

Can one use of a static variable, change it globaly? 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