- Home /
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()
Answer by Ashokkumar-M · Jun 28, 2017 at 09:46 AM
This error shows only 2 issues,
soundImage may not be assigned. or
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
Thank u for your answer, spriteSoundOn
is not null and soundImage
is 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 ?
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.