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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

dropdown call-by-reference working backwards in time?

Discussion in 'Scripting' started by tvalleau, Feb 21, 2019.

  1. tvalleau

    tvalleau

    Joined:
    Jul 1, 2010
    Posts:
    108
    Here's the short version:

    Using a GUI dropdown.value to index an array:
    Code (CSharp):
    1. string[] fracts = { "0", "1/16", "1/8", "3/16", "1/4"};
    2.  
    3. int mdv;
    4. string temp;
    5. m_Dropdown = GetComponent<Dropdown>();
    6. mdv = m_Dropdown.value; // (let's say it's 2)
    7. temp = fracts[mdv];
    8.  
    9. //let's see what we've got
    10. print (mdv); // 2
    11. print (temp); //  "1/8"
    12.  
    13. // let's reset the dropdown and check again:
    14.  
    15. m_Dropdown.value = 0;
    16. print (mdv); // 0
    17. print (temp); // "0"
    dmv and temp are NOT reassigned - they just change when I change m_Dropdown.value.

    I'm pretty sure that's never been my experience in my past; I was always able to get a value out of an array AND hang on to it in other languages.

    what am I missing here? Why is the call-by-reference rippling backward in time?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Post your full script as what is happening there makes no sense.
     
  3. tvalleau

    tvalleau

    Joined:
    Jul 1, 2010
    Posts:
    108
    "...makes no sense." I agree!

    Here's the shortest version of it that demonstrates the problem:

    Code (CSharp):
    1.  
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4.  
    5. public class HandleDropdown : MonoBehaviour
    6. {
    7.     //Attach this script to a Dropdown GameObject
    8.     Dropdown m_Dropdown;
    9.  
    10.     void Start()
    11.     {
    12.         m_Dropdown = GetComponent<Dropdown>();
    13.     }
    14.  
    15.     public void dealWithDropdown(int theNum)
    16.     {
    17.      
    18.         string bbb;
    19.         int m_DropdownValue;
    20.  
    21.         string[] fracts = { "0", "1/16", "1/8", "3/16", "1/4", "5/16", "3/8", "7/16", "1/2", "9/16", "5/8", "11/16", "3/4", "13/16", "7/8", "15/16" };
    22.  
    23.         m_DropdownValue = m_Dropdown.value; // the index of the list item, 0-15
    24.      
    25.         bbb = fracts[m_DropdownValue];
    26.  
    27.         Debug.Log("decfract1: " + bbb);
    28.  
    29.         m_Dropdown.value = 0;
    30.              
    31.         Debug.Log("decfract2: " + bbb);
    32.     }
    33. }
    for any m_dropdownValue != 0,
    defract1: does not equal defract2:

    for example if m_dropdownValue == 2, then bbb == "1/8" and the log decfract1 shows "1/8".
    After setting m_Dropdown.value = 0; however, the log decfract2 shows "0".

    it's behaving as if bbb now has the new index from m_Dropdown.value = 0.
     
    Last edited: Feb 21, 2019
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    is dealWithDropdown tied to the dropdown change event by any chance?
     
  5. tvalleau

    tvalleau

    Joined:
    Jul 1, 2010
    Posts:
    108
    OOPS... (sheepish grin). I got WAY too myopic and overlooked the obvious. You are entirely correct.
    Well, that leaves me with trying to figure out how to reset that darned dropdown and -not- trigger the activities inside dealWithDropdown when that happens... some global var I suppose.

    Well... sorry to trouble you, and appreciate your "clarity of vision" and your courtesy.

    Thanks.
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Why do you want to reset it? Without knowing more you could just return when the value is 0 as the event is a dynamic int. So...
    Code (CSharp):
    1.     public void dealWithDropdown(int theNum)
    2.     {
    3.         if(theNum == 0)
    4.             return;
    5.     }
     
  7. tvalleau

    tvalleau

    Joined:
    Jul 1, 2010
    Posts:
    108
    Yeah... I thought about that very thing, and was going to try it in a few minutes. The question is whether the user will think it weird that if they actually click on Zero, they don't get an entry. It's now a minor issue, however it plays out. (Thanks again.)

    As to why: the reason is that if it is not reset, then on the next use of the keypad, the previously selected item (say "1/8") is selected by default. Un-elegant as that is, I could live with that, except that if the user actually wants to enter "1/8" this time too, a click is produces nothing. He has to click on something else, and then click back on "1/8". So, I need a way to clear it. (Also because there is a "clear" button and that too needs to clear the dropdown.)

    Thanks.
     
  8. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Make zero the 2nd entry in the dropdown and when reset it says Set Fraction or something.
     
  9. tvalleau

    tvalleau

    Joined:
    Jul 1, 2010
    Posts:
    108
    Good idea. I think, however, I'm going to just default to zero, and let them change it if needed instead of selecting it.