Game Development
How to Change a Sprite in Unity 2D with a UI Button
A Unity button can swap the sprite displayed by a 2D object without replacing the GameObject. This tutorial shows how to assign two sprites, connect On Click(), and avoid two mistakes in the switching script.
A bowling ball sits at the left side of a small 2D scene. Clicking a button changes it from brown to dark blue. The ball stays where it is, with the same movement controls and physics. Its appearance changes without replacing the GameObject.
That last detail is what makes the example useful. The scene is keeping the same GameObject and replacing only the image rendered by one component.
The screenshots were captured in Unity 2020.1.17f1. The underlying approach also applies to newer Unity versions using SpriteRenderer and uGUI Button, although their Editor interfaces may differ.
What actually changes when a sprite changes?
A SpriteRenderer displays a Sprite asset. Its sprite property determines which image is rendered, so changing the appearance of a 2D object can be as small as assigning another reference:
spriteRenderer.sprite = secondSprite;
The object retains its position, scripts, and other components. In the bowling scene, the ball remains the player object; only its visible artwork changes.
This also explains why we should not confuse a Sprite with a SpriteRenderer. One is the asset we want to display, and the other is the component displaying it. They work together, but comparing the component to a sprite asset will not tell us which image is currently shown.
Prepare the object and two sprites
We begin with an existing bowling scene. The Player has a Sprite Renderer component, and two bowling-ball images are available in the project assets.
For your own scene, you need an object with SpriteRenderer and two Sprite assets to switch between. If you imported ordinary image files, check their import settings and make sure their texture type is Sprite (2D and UI).
The two images do not have to look alike, but different dimensions or pivots can make the object appear to jump or change size when you switch between them. For a simple color swap, two sprites with matching dimensions and alignment are easier to work with.
Create the ChangeSprite script
Create a C# script named ChangeSprite and attach it to the object whose appearance should change. Here, that object is Player.
Here is a corrected version of the logic shown in the video:
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
[SerializeField] private Sprite sprite1;
[SerializeField] private Sprite sprite2;
private SpriteRenderer spriteRenderer;
private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer == null)
{
Debug.LogError("ChangeSprite requires a SpriteRenderer.", this);
enabled = false;
return;
}
if (sprite1 == null || sprite2 == null)
{
Debug.LogError("Assign both sprites in the Inspector.", this);
enabled = false;
return;
}
spriteRenderer.sprite = sprite1;
}
public void Change()
{
if (spriteRenderer == null || sprite1 == null || sprite2 == null)
return;
spriteRenderer.sprite =
spriteRenderer.sprite == sprite1 ? sprite2 : sprite1;
}
}
There are two sprite references because the script needs to remember both possible appearances. The [SerializeField] attributes allow you to keep those fields private while assigning their values through Unity’s Inspector.
GetComponent<SpriteRenderer>() looks for a SpriteRenderer on the same GameObject that contains the script. If you attach ChangeSprite to a UI button instead of the player, that lookup will not find the player object’s renderer. In this example the script belongs on Player.
During Start(), the script retrieves the renderer, checks that its dependencies are present, and displays sprite1. The public Change() method then inspects the sprite currently displayed. If it is sprite1, it assigns sprite2; otherwise, it assigns sprite1.
The ternary operator is only a compact way to write that choice. It could equally be an if/else statement; neither requires an Update() loop.
Assign both sprite assets in the Inspector
Select Player in the Hierarchy. Under the Change Sprite (Script) component, Unity exposes the two serialized fields. Drag the first image to Sprite 1 and the second to Sprite 2.
The Player object has Change Sprite (Script) with two assigned sprites, alongside its existing Sprite Renderer.
If either field says None (Sprite), the script does not have everything it needs. That is why the example checks for missing assignments instead of silently attempting to use them.
Connect the button’s On Click event
Writing public void Change() does not make Unity call the method automatically. Something still needs to invoke it.
Here, the trigger is a UI button already placed on a Canvas. The button is named Change in the Hierarchy. You can use an existing button or create one through GameObject → UI → Button.
Select the button and look for its Button component in the Inspector. Under On Click(), add an event listener, then drag Player from the Hierarchy into the target-object field. Finally, choose ChangeSprite → Change() in the function dropdown.
The UI Button targets Player and invokes ChangeSprite.Change() when clicked.
The target must be the GameObject with the ChangeSprite component. Selecting the UI button itself as the target will not work unless you deliberately placed the script on that button and supplied a separate reference to the player’s renderer.
You will also notice that Unity can show several components belonging to the target object in the function menu. Choose the method from ChangeSprite, not a similarly named method from another component.
Check the result in Play Mode
Enter Play Mode and click the button. The bowling ball begins brown and turns dark blue after the click.
Before and after clicking the change button. The player object remains in place while its sprite changes.
Clicking again should return it to the first sprite. The script makes this decision from the currently assigned sprite, not from a separate boolean value.
This distinction is helpful if another part of your game can also replace the sprite. If the current image is something other than sprite1, the next click will select sprite1. It is a two-state toggle only while those are the two sprites involved.
Two mistakes hidden in the accompanying script
An earlier version of the example script contains this check:
if (spriteRender == null)
{
spriteRender.sprite = sprite1;
}
This asks Unity to assign a sprite precisely when the renderer is missing. If the condition is true, accessing spriteRender.sprite will fail. The intended condition is the opposite: assign the sprite only if the renderer exists. The version above handles that explicitly.
There is another line in the text file:
if (spriteRender == sprite1)
Here spriteRender is the SpriteRenderer component, whereas sprite1 is a Sprite asset. That is not the state we want to compare. The appropriate comparison examines the sprite inside the renderer.
if (spriteRenderer.sprite == sprite1)
The important part is the .sprite property. It gives us the image currently assigned to that renderer. Without it, the condition is checking the wrong things.
If clicking the button changes nothing
Start by checking the Console. A script that does not compile will not behave as expected, and a null reference can indicate a missing component or unassigned sprite field.
If the Console is clear, inspect the button’s On Click() event. Does it point to Player? Does it call ChangeSprite.Change()? Is the button interactable? In a standard Canvas-based UI, an active EventSystem is also needed to handle clicks.
Then check the type of graphics you are changing. The code above expects a SpriteRenderer on a scene GameObject. If your graphic is a UI Image under a Canvas, it uses UnityEngine.UI.Image, whose sprite property belongs to a different component. The same general idea works, but GetComponent<SpriteRenderer>() will not find a UI Image.
Finally, make sure the two sprite fields actually refer to different assets. Assigning the same image to both fields produces a working method with no visible difference.
The code is deliberately small. It does not move the ball, replace its GameObject, or perform animation. One button tells the player script to switch a single property, and Unity draws the newly assigned sprite on the next render. For a different trigger — a key press, collision, or game event — the same Change() method can be called from that other piece of logic.