Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by vanfriscia · Feb 07, 2018 at 06:20 PM · editor-scriptingui imageobject-reference-error

UI Images - Object reference not set to an instance of an object

Hi All,

Well, my initial post got zero responses and as a Unity noob I could really use some help. Essentially, I'm not understanding how variables assigned in the Inspector can turn out to be null at runtime.

I had functionality in a class that updated UI images on a canvas. These UI images were public variables that were assigned in the Inspector. It worked wonderfully, but really belonged in its own separate class; so, I created one and changed the original class to reference this new class to duplicate the functionality. However, once I separated it all out, I began getting the above object reference error.

My script bombs out on the following line:

 ScoreboardSlots [i + LeadingBlanks] = Instantiate (Digit0, Digit0.transform.parent);

ScoreboardSlots[whatever] is null, which is probably ok at this point because I'm trying to assign it to something. But, Digit0 is also somehow null even though it's been assigned in the editor as a public variable.

Surely I'm missing something very simple but I'm at a loss. Any advice???

Thanks, Todd

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vanfriscia · Feb 09, 2018 at 05:45 PM

Well I never did solve this, but I did implement a workaround. Instead of assigning my UI images in the Inspector via public variables, I changed them to private and created a new public method to initialize them like in the following example:

 Digit0 = GameObject.Find ("Digit0").GetComponent<Image> ();

This apparently HAS to be in its own method and called from another script; initially I tried putting it in Start() or Awake(), and while it didn't error out, the variables were again null once other methods were called. I still don't get it but maybe this will be helpful to someone, someday!

Comment
Add comment · Show 2 · 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
avatar image magmorta2012 · Aug 02, 2018 at 11:36 PM 0
Share

Thank you so much, this is exactly what I needed. I have no idea why GetComponent and dragging the image in to reference it was breaking it but this works. Very new to Unity so this was infuriating as I had no idea why the reference was disappearing when I pressed play haha.

avatar image vanfriscia magmorta2012 · Aug 08, 2018 at 11:47 AM 1
Share

I can relate to the fury my friend! But I'm glad to know that this post ended up helping SO$$anonymous$$EBODY even if it didn't help me :). Good luck!

avatar image
0

Answer by alti · yesterday

It's impossible to help you to debug without the script to look at. I'm only going to therefore assume what your problem is:

If you just write the variable as:

 GameObject[] ScoreBoardSlots;

You cannot fill it.

 void Start(){
 ScoreBoardSlots[0] = not allowed.
 }

You're trying to fill an array but you don't show us if the array was initialized. If an array isn't initialized, you cannot fill the numbered slot you listed above.

 private GameObject[] ScoreBoardSlots = new GameObject[2];
 void Start(){
 ScoreBoardSlots[0] = whateverGameObjectYouWant;
 }

Then you can fill it.

If this isn't/wasn't your issue, then you need to share more of the code.

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

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Level Selector - Level Images for Brick Breaker 2 Answers

Unity UI scale width or height without quality loss 0 Answers

Image to show icon works on one scene but not the other. 0 Answers

Show a photo when hitting a collider 1 Answer

Render to texture Artifacts 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