Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

error CS0122: 'Slider.Set(float, bool)' is inaccessible due to its protection level

Discussion in 'Scripting' started by gmfbrown, Jan 3, 2021.

  1. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    I'm trying to instantiate a series of sliders from a prefab and use some existing parameters to determine the initial value that each slider is set to. Here is the code I'm using:
    Code (CSharp):
    1.             amplitudeSliders[i] = Instantiate(amplitudeSliderPrefab, new Vector3(-350, 200 - 100 * i, 0), Quaternion.identity).GetComponent<Slider>();
    2.             amplitudeSliders[i].transform.SetParent(canvas.transform, false);
    3.             amplitudeSliders[i].Set(parameters[i][0], false);
    4.             amplitudeSliders[index].onValueChanged.AddListener((float value) => { cyclopods[index].ChangeAmplitude(value); });
    The third line in the code above produces an error with error message "error CS0122: 'Slider.Set(float, bool)' is inaccessible due to its protection level". I see in the Unity documentation that the Set method for a slider is, in fact protected rather than public. But whenever I've looked for solutions to the "is inaccessible due to its protection level" issue, it always comes down to changing the method from protected to public. But Sliders are Unity-owned classes, so I can't change the protection level of its methods. Is there some alternate way to set the initial position of the sliders according to a list of parameters?
     
    mc_fragezeichen likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Any reason you can't just set the .value property?
     
  3. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    Oops! For some reason, I misread the documentation and thought that property was read-only. Thanks!
     
    Kurt-Dekker likes this.