Author Topic: UIPopupList access value from script  (Read 3523 times)

jnapolit

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
UIPopupList access value from script
« on: February 24, 2017, 09:37:24 AM »
Hi,

I am trying to access the current value of a popup menu from a function however it is not letting me. I was wondering if anyone had any suggestions. I am working in C#.

I was able to make a public variable called Era, drag and drop the PopupList into that variable, then in my function I could use Era.value to get the value of Era but it would work only if I didn't change the value of the popupList. Once I changed the value, Era.value would just return null. I think I need to use Era.current.value but it won't let me because it's static.

I also tried UIPopupList Era = GetComponent<UIPopupList>(); but that gave me an error.

I think I am just missing something silly

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPopupList access value from script
« Reply #1 on: February 24, 2017, 10:09:35 AM »
This isn't an NGUI question. You can't declare a public field and use GetComponent on it... this isn't even a Unity question. It's a C# question. GetComponent is a member function of a MonoBehaviour class, and when you call it in a public field initialization, it's executed when the class gets created -- meaning there is no monobehaviour to work with... I don't even know how to explain this well as it's such a basic C# concept...
  1. void YourFunction ()
  2. {
  3.     Debug.Log(UIPopupList.current.value); // <-- will only work when called from inside a UIPopupList's callback function such as its onChange.
  4.     Debug.Log(GetComponent<UIPopupList>().value); // <-- will work from any script attached to the same object as the UIPopupList, provided you've set its 'Keep value' as true.
  5. }

jnapolit

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIPopupList access value from script
« Reply #2 on: February 24, 2017, 08:01:32 PM »
Hi,

I am sorry I think you misunderstood my question. What I explained in the first paragraph and then I commented that out and tried what was in my second paragraph and neither worked. I also tried your solution and it also does not work. Maybe I explained what I am trying to do poorly.

So I have this popup list, as well as an input box and a slider. In the input box the user is inputing a year, in the popup list they are picking BC or AD. As another way to manipulate which year they are in, the user can also use the slider. Now I want all three of these things to be connected, meaning if the user inputs a year, say 200, in the input box and chooses BC from the popup list, then the slider should move to the corresponding year. Similarly, if the user moves the slider to a particular year say 200 BC, then in the input box 200 should be displayed and the popuplist should then change to BC.

Does that make more sense? Right now I am able to get the slider and the input box's values and set them. However I can not do the same for the popup list. I have added my code below.

  1.  
  2. //this is called OnValueChange for the input box or when SliderInputChange calls it
  3. public void TimeInputChange(){
  4.                 //get the user input
  5.                 string yearstr = UIInput.current.value;
  6.                 int year;
  7.                 int.TryParse(yearstr, out year);
  8.                 //I only want the year to go from 400 BC to 400 AD
  9.                 if (year < 0 || year >400) {
  10.                         Debug.Log ("Please enter a positive year less than 400, use the drop down menu to navigate to BC or AD");
  11.                 }
  12.                 else {
  13.                         //this is the math which will take the year value and figure out where
  14.                         //the slider should be placed. So what I will need to do is if they pick BC
  15.                         //I will need to multiple year by -1 to get the correct sliderval, otherwise just
  16.                         //keep as positive for AD
  17.                         double slider = (year / 800.0) + 0.5;
  18.                         float sliderval = ((float)slider);
  19.                         YearSlider.Set (sliderval, false);
  20.                 }
  21.  
  22.         }
  23.  
  24.  
  25. //this is called OnValueChange for the slider or when TimeInputChange calls it
  26.         public void SliderInputChange(){
  27.                 //get the current slider value
  28.                 float yearslider = UISlider.current.value;
  29.                 //convert the current slider value to its corresponding year
  30.                 double year = (yearslider - 0.5) * 800;
  31.                 int yearint = (int)year;
  32.                 string yearStr = yearint.ToString();
  33.                 //put that year into the input box and in the future if this number was negative I
  34.                 //would just output the positive number to the input box but then change the popup
  35.                 //list to be BC instead of AD
  36.                 YearInput.Set(yearStr,false);
  37.                
  38.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPopupList access value from script
« Reply #3 on: February 26, 2017, 11:34:23 AM »
Grab the latest NGUI version and make sure that UIPopupList's "keep value" option is turned on. I'm guessing you're expecting for its value to be kept, while by default it's not. That option will let it be kept.

Alternatively, see http://www.tasharen.com/forum/index.php?topic=14267.0

jnapolit

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIPopupList access value from script
« Reply #4 on: March 01, 2017, 06:45:43 PM »
So I didn't buy NGUI I just downloaded the free version. So I can't just hit update in the asset store. I also can't seem to find the version that has the 'keep value' for free anywhere. Does a free version with 'keep value' exist?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPopupList access value from script
« Reply #5 on: March 06, 2017, 04:54:26 PM »
Free version is extremely old and doesn't come with support, but the answer to you question is no, that value doesn't exist in the free version.