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 RichardM · Sep 05, 2016 at 04:16 AM · unity 5error message

Why am I getting a "Object reference not set to an instance of an object"

I am working on a menu, using unity's ui. I have an empty scene, with a Canvas, a Panel and an image (square box). There use to be a lot more but this is all I need to get the error.

Attached to the canvas I have the following script attached:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class CharacterInventory : MonoBehaviour {
     public Image Socket0;
 
     void Awake () 
     {
         Socket0.sprite = Resources.Load<Sprite>("Sprites/spr-Shield-red");
     }
 }
 

The "box" is attached to the script in socket0. The "Red Shield" appears in the box as it should however I am getting :

NullReferenceException: Object reference not set to an instance of an object CharacterInventory.Start () (at Assets/Scripts/CharacterInventory.cs:9)

Anyone have any ideas as to why? Over 800 lines of code boils down to 1 line. I have tried a bunch of things but I keep getting the error and I have no idea why!!

Can anyone tell me what is causing this error, and how to fix it.

Thanks in advance

Richard M.

Comment
Add comment · Show 1
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 ScaniX · Sep 06, 2016 at 08:15 AM 0
Share

Is that really the correct script? The error message is saying something about the Start() method, but the above script does not even contain such a method.

When looking for errors it is helpful to post the exact script that is producing the problem and not post something similar that should contain the same error. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KyleRasmusson · Sep 06, 2016 at 06:17 AM

This will happen if you don't actually have something set in for the Object. So you're calling the object to do something, but its not actually there. You need to set the reference in the inspector, which I don't believe you're actually doing. So to get around this error, or at least have it stop throwing out the error, try doing this

      void Awake () 
      {
          if(Socket0 != null)
               Socket0.sprite = Resources.Load<Sprite>("Sprites/spr-Shield-red");
 
          else if(Socket0 == null)
               Debug.Log("Socket0 Not Found!");
      }

This wont actually pop in your Image, but it will stop throwing the error, and it will let you know that your image isn't there.

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

6 People are following this question.

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

Related Questions

Unloading Broken Assembly | How to solve this !? 2 Answers

unity photon network does not work. any answers? 1 Answer

error CS0118: `UnityEngine.Object.Destroy(UnityEngine.Object, float)' is a `method' but a `type' was expected problem 1 Answer

cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers

How I can fix "windows cannot acces......." error? 0 Answers


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