Author Topic: Popup List forgetting items.  (Read 6339 times)

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Popup List forgetting items.
« on: June 17, 2014, 10:47:43 AM »
I'm using a popup list to swap sprites over from an array of sprites set in the inspector. It works fine until I reopen the scene and then it seems to forget the array, even though all the items are still set in the inspector.

Here's the error:

Quote
NullReferenceException: Object reference not set to an instance of an object
imagePopUpListSwitch.ShowGraphic () (at Assets/imagePopUpListSwitch.cs:25)
imagePopUpListSwitch.OnSelectedIndexChanged () (at Assets/imagePopUpListSwitch.cs:19)
EventDelegate.Execute () (at Assets/NGUI/Scripts/Internal/EventDelegate.cs:455)
EventDelegate.Execute (System.Collections.Generic.List`1 list) (at Assets/NGUI/Scripts/Internal/EventDelegate.cs:598)
UIPopupList.TriggerCallbacks () (at Assets/NGUI/Scripts/Interaction/UIPopupList.cs:276)
UIPopupList.set_value (System.String value) (at Assets/NGUI/Scripts/Interaction/UIPopupList.cs:217)
UIPopupList.Start () (at Assets/NGUI/Scripts/Interaction/UIPopupList.cs:397)

Here's my script:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class imagePopUpListSwitch : MonoBehaviour {
  5.  
  6.     public GameObject[] spritesToSwitch;
  7.  
  8.     UIPopupList popupList;
  9.  
  10.     int previousGraphic = 0;
  11.  
  12.         // Use this for initialization
  13.         void Start () {
  14.         popupList = gameObject.GetComponent<UIPopupList>();
  15.         }
  16.  
  17.     public void OnSelectedIndexChanged()
  18.     {
  19.         ShowGraphic();
  20.     }
  21.  
  22.     void ShowGraphic()
  23.     {
  24.         spritesToSwitch[previousGraphic].SetActive(false);
  25.         spritesToSwitch[popupList.items.IndexOf(popupList.value)].SetActive(true);
  26.         previousGraphic = popupList.items.IndexOf(popupList.value);
  27.     }
  28. }

No doubt I have something set up wrong but it's working for other elements just fine, apart from this one popuplist.  :-\

Any suggestions please?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Popup List forgetting items.
« Reply #1 on: June 17, 2014, 10:56:07 AM »
"forgets the array"? What array? Popup list stores items as a list, and this list is specified on the scroll view's script. If you modify it in the scene then navigate away from that scene, then of course your changes will be discarded when you go back. You'd have to save them somewhere for them not to be discarded.

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Popup List forgetting items.
« Reply #2 on: June 18, 2014, 04:29:37 AM »
I'm adding 5 items in the GameObject array via the inspector (see attached screen), those items are sprites that I want to show or hide using SetActive(true) or SetActive(False).

I'm using the Index of to get the selected item in the popuplist and to use that in the array of gameobjects.

I save my scene with all the GameObjects set & the scene working how I want, when I reopen the scene, even though the array is still populated in the inspector I get the above error.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Popup List forgetting items.
« Reply #3 on: June 18, 2014, 03:46:13 PM »
Why don't you add some null checks in there? Don't assume that you'll be getting valid values. Figure out which value is actually null.

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Popup List forgetting items.
« Reply #4 on: June 18, 2014, 04:51:33 PM »
Why don't you add some null checks in there? Don't assume that you'll be getting valid values. Figure out which value is actually null.

I did, they're not null in my script.  :-\