Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k 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 Reply

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]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...