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.
Not tested…but it should work :
public GameObject go;
string[] Array = yourText.Split("
" [0]);
for (int i = 0; i< Array.Length ; i++) {
GameObject someObject = (GameObject)Instantiate(go);
someObject.name = Array*;*
}
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.
String.Split with the newline character will give you an array of strings that you could then loop through