Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

unity3d - ForEach Index value in C#

I have an int list with size 2. And inside this list have a value of 1 , 3 and an int array 0,1,2,3,4,5

How could I increas the value of array 1 and 3 to with the value of the int list?

Exemplifying what I was trying but it didn't work

for (int index = 0; index < list.Count; index++)
{
    session.score_kills[index] += 3;
    Debug.Log(index);
}

But this loop he puts the value in the int array 0,1,2 and not only 1,3 which is the list aiming at the order (counting) of the list in ascending order

I also tried:

foreach (var x in list)
{
    session.score_kills[x] += 3; Debug.Log(x)
}

but it is only in the list size and not in the int value inside it. Sorry for the bad English, I'm trying my best to explain based on what I know of the language. I thank everyone who can help me, I'm still new. If any information is missing please let me know and excuse me beforehand I'm already 6 days in this problem and tired


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var indeces = new list<int> {1, 3};

// iterate through the list of indeces to find each index
foreach (var index in indeces) {
    session.score_kills[index] += 3;
    Debug.Log(index + " -> " + session.score_kills[index]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...