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 Morwin25 · Jun 28, 2017 at 09:24 AM · spritescript.imagenullreferenceexception

Problem with Image and Sprite

Hi all, I have one question that i have tried to resolve but i could not.

I have this var: public Image soundImage; (I drag and drop the image in the inspector)

And i call a function to switch the sound sprite when i press a button. The function contains :

 if (sound) {
             soundImage.sprite = spriteSoundOn;
             sound = false; 
 }else{
             ....

The var spriteSoundOn it is also in the inspector ( I tried with spriteSoundOn = = Resources.Load<Sprite> (sprite path); but it did not work

The problem gives me unity in the line soundImage.sprite = spriteSoundOn; is:

NullReferenceException: Object reference not set to an instance of an object GameState.Sound () (at Assets/Scripts/GameState.cs:118) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

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
1

Answer by Ashokkumar-M · Jun 28, 2017 at 09:46 AM

This error shows only 2 issues,

  1. soundImage may not be assigned. or

  2. spriteSoundOn may not be assigned.

either one of the above will cause the error,

You have mentioned you are drag and dropping image for sound image in inspector.

So, the only error could be spriteSoundOn might have been unassigned or null. You have mentioned ( I tried with spriteSoundOn = = Resources.Load (sprite path);)

Solution: 1.Check whether the name of path is correct with the sprite in the resource folder. or 2.Expose the spriteSoundOn variable in inspector and assign it with the sprite.

To double check it print log and see if spriteSoundOn is null or soundImage is null. @Morwin25

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 Morwin25 · Jun 28, 2017 at 11:25 AM 0
Share

Thank u for your answer, spriteSoundOn is not null and soundImageis null. If with the inspector is still null, I try to do it with findgameobjectswithtag ??

EDIT: I tried with :

 if (GameObject.FindGameObjectWithTag ("sound") == null) {
             Debug.Log ("is null");
         } else {
             GameObject im = GameObject.FindGameObjectWithTag ("sound");
             if (im.GetComponent<Image>() == null) {
                 Debug.Log ("is null");
             } else {
                 im.GetComponent<Image> ().sprite = spriteSoundOff;
             }
         }

it works!!! but i think that FindGameObjectWithTag is very slow for mobile devices. Dont u think ?

avatar image Ashokkumar-M Morwin25 · Jun 28, 2017 at 01:06 PM 0
Share

Yes it will be slightly slow, If you can share the screen shot of how you are trying to assign soundimage in Inspector the I can help you with it.

Probably you are assigning it to the prefab under Projects and trying to load gameobject in Hirearchy, which may not be linked.

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

89 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

Related Questions

How create a audience style game Rocksmith 1 Answer

Canvas image sprite change from script 0 Answers

Change UI Image through a script with AssetDatabase.LoadAssetAtPath 0 Answers

Error facebook profile picture 1 Answer

Crop UI image and convert it as Sprite 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