start function stops midcode when setting slider value

In my code i set two slider values (this happens in the start function) but for somehow it didnt work. i checked it and found out the code below it doesnt get called either. so i placed Debug.Logs to check where it is and ended up with these two lines. (when removing these the code worked again)

 //mouseSpeed and volume are both an INT
Debug.Log("works1");
mouseSpeedSlider.value = mouseSpeed;
volumeSlider.value = volume;
Debug.Log("works2");

Debug.Log(“works2”); is never shown in the console

(i also tried using floats as value with no succes)
it doesnt show any errors or warnings in the console.

if someone could explain me why this isnt working and how i should fix it that would be nice!

In the upper right corner of your console window is a little toggle that can be flipped to hide errors. Make sure it is on.

Are you sure there are no exceptions being thrown when this runs?

heres a image of the console. nothing is hidden as far as i can tell.

and what do you mean with:

3444656--272661--error unity.PNG

The error says something is accessing clickEffect of the “menu-something,” but the graphic is cut off.

None of those identifiers are in the script above. Are you using another menu manager package that might add scripts to your UI objects? Do the identifiers in your script snippet above map to properties that use those identifiers?

You probably forgot to set the reference to the object in the inspector, how do you declare them?

the error is caused because its trying to acces a object which is declared after the code i showed in the picture.

i have them selected in the inspector (shown in the image below)

and like this in the code:

public int mouseSpeed;
public int volume;
public Dropdown resDropdown; //this is not part of the problem
public Dropdown qualityDropdown; //this is not part of the problem
public Slider volumeSlider;
public Slider mouseSpeedSlider;

3444843--272703--error unity2.PNG

Can you post the full script and indicate which line has an error (if one does)?

Note that it is normal for the rest of a function to not execute after hitting a null or unassigned reference error. You go to the line of code indicated by the error, figure out what references are being used on that line, and then figure out which one is null. Then make it not null.

1 Like

line 42: “clickEffect = this.gameObject.GetComponent();” is never called. because of this line 83 “clickEffect.Play();” (and everywhere i have this code) is returning a error (cant play if the clickEffect isnt set).
(line 24 is temporary public and is normaly private but i changed that while testing)

everything below line 39 “volumeSlider.value = volume;” isnt called or executed.
when i remove line 38 and 39 (the ones i showed in the question, marked as line 3 and 4) it works perfectly.

when removing only 1 of them the problem is still there so both of them are causing it.

so i know the problem is caused by these 2 lines. now i checked if i had the correct sliders selected in the inspector. which i have.

both are set in the inspector. and both are causing the problem because when i remove one of them it still wont work but when removing both everything works.

3445261–272782–MenuController.cs (3.62 KB)

You probably have UnityEvent on the slider which tries to set the volume on the clickEffect?
When you set the value of the slider it will try to call that event but the clickEffect isn’t set yet. Probably. Or something like that.

Move the

clickEffect = this.gameObject.GetComponent<AudioSource>();

line above the

volumeSlider.value = volume;

line and see what happens.

1 Like

thanks alot. it works now. i didnt know it would call the event when it was changed by code. again thanks alot!