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 z25assassins · May 09, 2017 at 06:10 AM · uibuttonlistinputfielddropdown

DropDown menu(add item from input field): Object reference not set to instance of an object C#

My goal is: Type into the input field -> press the button "Enter" -> have that added to the drop down list.

My question is -> How do I get rid of this NullReferenceException Error?

And if at all possible, how can I make this work?

alt text

The Error I am getting is this.

NullReferenceException: Object reference not set to an instance of an object DdMenu.addItem()(at Assets/Scripts/DdMenu.cs: 20)

Here are my two scripts.

First script : This is for adding the options into the dropdown menu

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DdMenu : MonoBehaviour {
 
     AddOption ao;
     public Text selectedName;
     public Dropdown drop;
 
 
     public void dropIndex(int varIndex){
 
         selectedName.text = ao.menuList[varIndex];
     }
 
     public void addItem(){
 
         drop.AddOptions (ao.get_holder());  //Error is here
     }
 
 
 }


Second Script: This is for getting the text from the input field.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class AddOption : MonoBehaviour {
     public List<string> menuList;
     public Text test; 
 
     private string x;
     public void getText(){
 
         x = test.text;
         menuList.Insert (0, x);
 
     }
 
     public List<string> get_holder(){
         return menuList;
     }
 }

screenshot-103.png (45.7 kB)
Comment
Add comment · Show 2
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
avatar image z25assassins · May 09, 2017 at 02:40 PM 0
Share

I figured it out actually. Ill post the answer later.

avatar image z25assassins · May 09, 2017 at 04:51 PM 0
Share

Watch my video on how to do this... https://youtu.be/wIxYihzCFs$$anonymous$$

View the Source Code Here :

https://zakalberda.com/2017/05/09/unity-dropdown-add-and-delete-with-a-button/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by phxvyper · May 09, 2017 at 04:15 PM

Read the error. One of the objects you're attempting to access data from is null.

On line 20

 drop.AddOptions (ao.get_holder());

you have two objects you're attempting to act upon: drop and ao.

You have to assign these objects either in code or in the editor before attempting to use them like this.

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to play scrolling sounds when scrolling through ScrollRect? 1 Answer

Help With Adding Buttons To Panel 4 Answers

List to dropdown 0 Answers

A node in a childnode? 1 Answer

dropdown menu after effects 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