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
1
Question by AndreasX12 · Aug 26, 2014 at 06:19 PM · stringloop

Loop through string? (foreach new line)

Hi.

Is there a C# script that makes it possible to make a for loop that loops through a string each new line.

For example if we have a string like this (with 2 lines):

 A dog
 A cat

I would like a for loop that can loop though the lines and for example instantiate a gameobject with the name 'A dog' or 'A cat'.

Please tell me if this isn't explained correctly.

Thank you very much.

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

3 Replies

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

Answer by superbsumit · Aug 26, 2014 at 06:51 PM

Not tested..but it should work :

 public GameObject go;
 string[] Array = yourText.Split("\n" [0]);
 for (int i = 0; i< Array.Length ; i++) {
 GameObject someObject = (GameObject)Instantiate(go);
 someObject.name = Array[i];
 }

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 AndreasX12 · Aug 26, 2014 at 08:35 PM 0
Share

Thank you! :)

Got it working :)

avatar image
0

Answer by fueldown · Aug 26, 2014 at 06:29 PM

If you are reading that from a file, I suggest you to use the CSV and parse the csv with buffered reader.

If you are not, then this is very odd thing to do. you may need to breakdown the string using some special character to separate the lines and saving it in string array. Then iterate that array with foreach / for loop.

Comment
Add comment · Show 2 · 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 AndreasX12 · Aug 26, 2014 at 06:38 PM 0
Share

Thanks for your answer.

It's because I'm getting the string using the WWW class and there is one word on each line. I then want to create a GameObject for each line with a name according to the string on that line. I hope this makes more sense.

avatar image fueldown · Aug 26, 2014 at 06:45 PM 0
Share

@brianruggieri is right. I was suggesting the same thing, but wasn't sure if newline character (\n or \r still not sure) can be used with String.Split. If you can, then that's the way to go. If not, and you have access to changing what you can receive, you may want to change the newline to a special character and use that with String.Split.

 A Cat|A Dog|... 

avatar image
0

Answer by roojerry · Aug 26, 2014 at 06:32 PM

String.Split with the newline character will give you an array of strings that you could then loop through

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 jokim · Aug 26, 2014 at 06:43 PM 0
Share

Assu$$anonymous$$g the newline character is \n :

 foreach (string line in String.Split(myString, "\n"))
 {
     GameObject obj = new GameObject(line);
 }

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

25 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

Related Questions

Using LINQ within a foreach only runs once 1 Answer

Array strings/Loops problem 2 Answers

Longest Common Subsequence Recursive Function Error. 1 Answer

Stop working after for loop appending string 1 Answer

How to make AudioSource play defined clips using String? 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