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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

start function stops midcode when setting slider value

Discussion in 'Scripting' started by kelvin-w, Mar 30, 2018.

  1. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    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)

    Code (CSharp):
    1.  //mouseSpeed and volume are both an INT
    2. Debug.Log("works1");
    3. mouseSpeedSlider.value = mouseSpeed;
    4. volumeSlider.value = volume;
    5. 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!
     
    Last edited: Mar 30, 2018
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    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?
     
  3. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    heres a image of the console. nothing is hidden as far as i can tell.

    and what do you mean with:
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    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?
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You probably forgot to set the reference to the object in the inspector, how do you declare them?
     
  6. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    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:


    Code (CSharp):
    1. public int mouseSpeed;
    2. public int volume;
    3. public Dropdown resDropdown; //this is not part of the problem
    4. public Dropdown qualityDropdown; //this is not part of the problem
    5. public Slider volumeSlider;
    6. public Slider mouseSpeedSlider;
     

    Attached Files:

  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Can you post the full script and indicate which line has an error (if one does)?
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    Kurt-Dekker likes this.
  9. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    line 42: "clickEffect = this.gameObject.GetComponent<AudioSource>();" 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.
     

    Attached Files:

  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    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
    Code (CSharp):
    1. clickEffect = this.gameObject.GetComponent<AudioSource>();
    line above the
    Code (CSharp):
    1. volumeSlider.value = volume;
    line and see what happens.
     
    kelvin-w likes this.
  11. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    thanks alot. it works now. i didnt know it would call the event when it was changed by code. again thanks alot!