Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cosmin.cretu · Dec 19, 2015 at 08:40 AM · listruntimeeditorgui

Creating a UI list at runtime programmatically

I have a scene where I should be able to select a 3D model from a list of items, which will then be loaded into the next scene. I want to generate the list of items programmatically, based on what is already stored in the local database. But I have problems getting the list of items to be displayed in the scene ...

So I have a class for the items:

 [System.Serializable] public class Item { 
 public string ObjectGuid;
     public string Name;
     public string Description;
     public Sprite Icon;
     public Button.ButtonClickedEvent SelectObject; }

A script for my scene, that contains a list of items and a method that I call to load the objects from the database:

 public class ObjectSelectionScript : MonoBehaviour {
 public List<Item> itemList;
 public Transform contentPanel;
 
     void LoadObjects() {
         StreamReader str = new StreamReader (Application.persistentDataPath + "/" + DataManager.LOCATION_XML_PATH);
         string result = str.ReadToEnd ();
         str.Close();
         var jo = SimpleJSON.JSON.Parse (result);
 
         if (jo ["Objects"] != null) {
             for (int i = 0; i < jo["Objects"].Count; i++) {
                 if (!string.IsNullOrEmpty(jo["Objects"][i]["FileGuid"])) {
                     Item newObject = new Item();
                     newObject.ObjectGuid = jo["Objects"][i]["ObjectGuid"];
                     newObject.Name = jo["Objects"][i]["Name"];
                     newObject.Description = jo["Objects"][i]["Description"];
                     //newObject.SelectObject = new Button.ButtonClickedEvent();
                     itemList.Add(newObject);
                 }
             }
         } 
 }

A ModelObject class:

 public class ModelObject : MonoBehaviour {
     public Button button;
     public Text Name;
     public Text Description;
     public Image Icon;
 }

And a prefab ModelObjectPrefab that contains Name, Description, a Thumbnail, added to my assests folder.

The PopulateList() method is my problem. I can't get to instatiate the prefab and then create ModelObjects that I can throw in the contentPanel in the interface. This is my code:

  void PopulateList()
     {
         try {
             foreach (var item in itemList)
             {
                 GameObject newModel = Instantiate(ModelObject) as GameObject;
                     ModelObject myModelObject = newModel.GetComponent<ModelObject>();
                     myModelObject.Name.text = item.Name;
                     myModelObject.Description.text = item.IDescription;
                     myModelObject.icon.sprite = item.Icon;
                     myModelObject.button.onClick = item.selectObject;
                     newModel.transform.SetParent(contentPanel);
                 
             }
             
         } catch (System.Exception ex) {
             
         }
     }
 

I currently get null pointer on instantiate. I have tried Resources.Load, and variations of the Instatiate method, but I did not find a solution.

Can anyone give me a hint what is the problem? How should PopulateList() look like to get the items to the interface?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Add all objects of a type to a list before runtime 1 Answer

Array of vectors lost in runtime, but not null 0 Answers

How to pull two objects at the same time? 0 Answers

(Editor Scripting) How to create a list view 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges