- Home /
Array Element in Foreach loop
So I am trying to track which array element is running in a foreach loop. How can i do that/
Here is an exert of my script:
void Update (){
if (tracker.tracking == true)
{
if (pageInitialized == false )
{
foreach (Book book in books)
{
foreach (string page in book.page)
{
if (tracker.targetName == page)
{
current = page;
//What I am trying to do is get what element of the array page is.
// then plug it in to the following line similar to this.
//downloader.BundleURL = PageURL[currentelement]
StartCoroutine (downloader.Download());
pageInitialized = true;
StopCoroutine (waitForReset());
waiting = false;
}
}
}
}
}
}
Where I have the '//' at explains what i am trying to do and where. If anyone knows and can help out that would be greatly appreciated.
Comment
Answer by MonkeyHood · Jul 27, 2015 at 10:21 PM
You would need to create your own variable to track where you are in the loop. Foreach uses an enumerator and can't know where it is in the array.
Try something like this: How do you get the index of the current iteration of a foreach loop?
Ok Thanks. That did make it work. I really appreciate. I voted your answer thumbs up :)