Fill Lists recursevely
Hi all, I am trying to map elements of a list into another list. I have succeed doing it manually(see the commented part) but I really want to do it recursively. Can someone tell me how to do it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MidiJack;
using UnityEngine.UI;
public class MidiControlMapping : MonoBehaviour {
//public int[] knobNumber;
public List <int> myknobs;
public List <Slider> mysliders;/
//public Slider mySlider;
void Start()
{
//sliders = new List<Slider>();
}
void Update()
{
SetSlider ();
}
public void SetSlider()
{
foreach(int i in myknobs)
{
mysliders[i].value = MidiMaster.GetKnob(myknobs[i]);
}
//mysliders[0].value = MidiMaster.GetKnob(knobNumber[0]);
//mysliders[1].value = MidiMaster.GetKnob(knobNumber[1]);
//mysliders[2].value = MidiMaster.GetKnob(knobNumber[2]);
//Debug.Log("You are using knob number = " + knobNumber);
}
}
What problem are you facing? Have you any error in the console?
I get: ArgumentOutOfRangeException: Argument is out of range.
In short how do I assign the first element of a list to the first element of a second list, the second element of of the first list with the second element of the second list and so on...but recursively?
I'm not sure you understand what "recursively" means.
Here, your problem has nothing with recursion. Your mySliders list is simply not big enough for all your knobs.
You may want to do :
private List <Slider> mysliders;
public void SetSlider()
{
mysliders = new List<Slider>();
foreach(int i in myknobs)
{
mysliders.Add( $$anonymous$$idi$$anonymous$$aster.Get$$anonymous$$nob(myknobs[i]) ) ;
}
}
Ok but now I have:
Assets/Scripts/$$anonymous$$idiControl$$anonymous$$apping.cs(44,14): error CS1502: The best overloaded method match for `System.Collections.Generic.List.Add(UnityEngine.UI.Slider)' has some invalid arguments
and
Assets/Scripts/$$anonymous$$idiControl$$anonymous$$apping.cs(44,30): error CS1503: Argument `#1' cannot convert `float' expression to type `UnityEngine.UI.Slider'
mySliders and myknobs need to be public because I need to add them in the editor.
I thought recursively meant when you don't do it manually but usually with a for loop, if not I am sorry I am not a programmer
You have to change the type between the <> symbols so as to match the type returned by $$anonymous$$idi$$anonymous$$aster.Get$$anonymous$$nob
Recursion in program$$anonymous$$g means an other things check the wikipedia page or this link which is simpler.
Yeah of course sorry I meant iteration not recursion... anyway now it works...sort of, no error but the numbers of sliders all ways stays at 0
$$anonymous$$ake sure the myknobs list is not empty and $$anonymous$$idi$$anonymous$$aster.Get$$anonymous$$nob returns elements correctly.

As You can see I have re-filled the list in the editor, but now it went back to the original error
If you do it through the inspector, you don't need to call SetSlider by code anymore.
Your answer