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 thorskull · Jun 20, 2013 at 06:24 PM · iforder-of-execution

How to make if/else statements progress in order

I have a GUIText that I am clicking that starts out with the text "Lateral Incision". What I'm aiming for is making it so that every time I click the text, the text changes to the next scripted statement. The problem now is that it starts and immediately jumps to the end. I haven't found anything helpful in other answers so was just wondering if someone knew what to do? I'm thinking it's something quite simple, but I can't figure it out.

Here's the code: if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){

             simtext.text = ( "Mark Incision");
         }
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
              simtext.text = ("Incise Skin & \nSubcutaneous Tissue");
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Perform 'H-cut'; \nwatch for peroneal nerve";
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Lateral Incision Complete";
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Medial Incision";
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Mark Incision";
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Expose & Free Muscles";
         }
         
         if (simtext.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0)){
             
             simtext.text = "Fasciotomy Complete";
         }
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
1
Best Answer

Answer by HenryStrattonFW · Jun 20, 2013 at 07:04 PM

This will just make your text read the final value. That is because every condition is the same, so all of them will fire as the code runs down them.

What i would suggest is this, instead of hard coding the text values into the statements, put them into an array. then store a "currentIndex" which will start at 0. Then when the button is pressed you increase the index and set the text. here is an example.

     var myStrings = new Array("String 1","String 2","String 3");
     var currIndex = 0;
     
     
     function Update()
     {
          if(myGUIText.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
          {
               currIndex++;
     
               // here you will want to also check current index to either
               // wrap it back around to 0 or clamp it on the last value
               // based on the array length.
     
               myGuiText.text = myStrings[currIndex];
          }  
     }
Comment
Add comment · Show 5 · 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 thorskull · Jun 20, 2013 at 07:18 PM 0
Share

That would work great if I were using JS but for the project I can only use C# (it's a requirement). Is there anything comparable to this for C#?

avatar image Eric5h5 · Jun 20, 2013 at 08:05 PM 0
Share

Don't use the JS Array class even in Unityscript. It's slow, untyped, and obsolete. Use built-in arrays or generic Lists. In this case it should be

 var myStrings = ["String 1", "String 2", "String 3"];

which can be done in C# but with different syntax.

avatar image thorskull · Jun 20, 2013 at 08:38 PM 0
Share

Is there anyway you could provide me with an example? I've only been working with Unityscript for the past week and a half so I'm not familiar with the more complex commands in C#. If you could I'd really appreciate it.

avatar image HenryStrattonFW · Jun 20, 2013 at 09:04 PM 0
Share

my apologies for some reason i thought you were using javascript which looking back at your post im not sure why I thought that, i always go C# myself..

only needs $$anonymous$$or changes for that just declare the array and index with the appropriate syntax

 public string[] myStrings = new string[]{ "string one", "string two", "etc" };
 public int currIndex = 1;

oh and function update() becomes void Update()

sorry for the mistake

avatar image thorskull · Jun 20, 2013 at 09:36 PM 0
Share

Thank you so much! I had one small problem where it was skipping my first string, I had to move my string script into the Update for it to be recognized. But besides that it works great!

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

17 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

Related Questions

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

Scripting these if/else statements to operate correctly 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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