• 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 Baniucha · Jun 29 at 12:10 PM · arraystringif-statementsloops

How to display a certain string out of array depending on a number?

Hello, I am trying to create a week system Monday through Sunday and I have these names in an array of string and I want them to be displayed accordingly to the number of that day. However, whenever I try to create a loop for it, something goes wrong.

 public TextMeshProUGUI dayText;
 public string[] dayName;

 public int countDays;
 public int dayNo;
 bool test;
 // Start is called before the first frame update
 void Start()
 {
     dayName[0] = "Monday";
     dayName[1] = "Tuesday";
     dayName[2] = "Wednesday";
     dayName[3] = "Thursday";
     dayName[4] = "Friday";
     dayName[5] = "Saturday";
     dayName[6] = "Sunday";
     dayNo = 1;
 }

 // Update is called once per frame
 void Update()
 {
     CountDays();
 }
 public void CountDays()
 {
     //TO DO 
     //DISPLAY NAME OF THE DAY ACCORDING TO THE NUMBER
     if (Input.GetKeyDown(KeyCode.A))
     {
         test = true;
     }
     if (test)
     {
         for (int i = 0; i < 7; i++)
         {
             dayText.text = dayName[i];
             print(i);


         }
     }
 }

As you can see in CountDays() function I was trying to put a condition to break out of the loop but immediately when I press "A" it changes from Monday to Sunday and I want it to be Monday -> Tuesday -> Wednesday etc. Please and thank you in advance for any help

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

Answer by TheTrueDuck · Jun 29 at 01:41 PM

Hi! Instead of doing your for loop, try simply incrementing the dayNo variable and printing that out:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.A))
     {
         dayNo += 1;
 
         dayText.text = dayName[dayNo];
         Debug.Log(dayName[dayNo]);
     }
 }

The problem was that you were going through all 7 days in one click instead of one at a time.

You'll notice the above code will run into an error once this number goes past 7, but you can loop it around with the fancy modulo (%) operator:

 dayNo = (dayNo + 1) % 7;

Hope this helps :D

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 Baniucha · Jun 30 at 01:10 PM 0
Share

Sir, you are a Saint and a Scholar. Thank you so much for your help :D Not only after "Sunday" it loops back to "Monday", I also added a day number to each day. I'm attaching a full code if anybody runs into similar problem:

  if (Input.GetKeyDown(KeyCode.A))
         {
             if (countDays >= 7)
             {
                 countDays = 0;
             }
             else
             {
                 countDays = (countDays + 1) % 7;
                 dayNo++;
                 dayText.text = dayName[countDays] + "\n" + "Day " + dayNo;
                 Debug.Log(dayName[countDays]);
             }
         }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

152 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 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 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 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 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 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 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

Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers

how can i split a word to individual letters..?? 2 Answers

Array Slit String and Send NGUI text 0 Answers

Newbie Problem with Multidimensional Arrays in JavaScript 0 Answers

Split String into Arrays 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