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 /
  • Help Room /
avatar image
0
Question by Andrew_Buiko · Mar 02, 2017 at 04:57 PM · uigameobjectspritenullreferenceexceptionui image

Object reference not set to instance of an object?

I'm not sure why, but I keep on getting an error regarding to NullReferenceException in my script for a game that I'm working on. It keeps on referring to line 96 (or //KeyDisplay.sprite = Resources.Load ("pixel_key") as Sprite;) I try to display a sprite on my UI whenever I walk to pick up a key. On that collision it should be updating the sprite on the UI after colliding with the key, but it just throws that exception out at me. Please help!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class Move : MonoBehaviour
 {
     private SpriteRenderer spriteRenderer;
     public Sprite Character_Back, Character_Front, Character_Left, Character_Right, pixel_key;
     public float speed = 1.5f;
     public Vector2 tempSpeed;
     private int count;
     public Text amtOfKeys;
     public Image KeyDisplay;
 
     Animator animator;
 
     void Start ()
     {
         animator = GetComponent<Animator>();
         spriteRenderer = GetComponent<SpriteRenderer>();
         if (spriteRenderer.sprite == null)
             spriteRenderer.sprite = Character_Front;
         count = 0;
         amtOfKeys.text = "";
     }
 
     void Update ()
     {
         
         animator.SetBool("Up", false);
         animator.SetBool("Down", false);
         animator.SetBool("Left", false);
         animator.SetBool("Right", false);
 
         if (Input.GetKey (KeyCode.A)) 
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
             animator.SetFloat ("X-Speed", -speed);
             tempSpeed.x = -speed;
         } 
         else if (Input.GetKey (KeyCode.D)) 
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
             animator.SetFloat ("X-Speed", speed);
             tempSpeed.x = speed;
         }
 
         if (Input.GetKey (KeyCode.W))
         {
             transform.position += Vector3.up * speed * Time.deltaTime;
             animator.SetFloat ("Y-Speed", speed);
             tempSpeed.y = speed;
         }
         else if (Input.GetKey (KeyCode.S))
         {
             transform.position += Vector3.down * speed * Time.deltaTime;
             animator.SetFloat ("Y-Speed", -speed);
             tempSpeed.y = -speed;
         }
 
         if (tempSpeed.y > 0) 
         {
             animator.SetBool ("Up", true);
         } 
         else if (tempSpeed.y < 0) 
         {
             animator.SetBool ("Down", true);
         } 
         else if (tempSpeed.x > 0) 
         {
             animator.SetBool ("Right", true);
         } 
         else if (tempSpeed.x < 0) 
         {
             animator.SetBool ("Left", true);
         } 
         else 
         {
         }
     }
 
     void OnTriggerEnter2D(Collider2D other) 
     {
         if (other.gameObject.CompareTag("Key"))
         {
             other.gameObject.SetActive(false);
             count++;
             SetAmtOfKeys ();
         }
     }
 
     void SetAmtOfKeys()
     {
         amtOfKeys.text = "\tKeys : " + count.ToString ();
         //KeyDisplay.sprite = Resources.Load<Sprite> ("pixel_key") as Sprite;
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Commoble · Mar 02, 2017 at 05:11 PM

You need to link your Image component to your script in the inspector. You can do this by dragging from the Image component to the KeyDisplay field in your script when viewing the script component in the inspector. If the Image is on another GameObject, you can drag the Image's GameObject to the KeyDisplay field, or click the little menu icon next to the KeyDisplay field and select from all Images in your scene.

Comment
Add comment · Show 1 · 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 Andrew_Buiko · Mar 02, 2017 at 05:55 PM 0
Share

I did this, and after doing so, it still keeps the Exception active. It's not updating the sprite on the UI layer. (This is after running the game afterward.)

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

158 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 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

Sprite Renderer and Image 0 Answers

is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer

Unity2D UI Image sprite to fill frame 1 Answer

UI Image Cutout / Masking in spefic areas with sprites 0 Answers

How to crop an alpha UI image? 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