0
\$\begingroup\$

I'm trying to create a button within the settings menu that allows you to change the image of a GameObject per click. So, let's say there are only 2 different images available to choose from and it starts with Image1, if you press the button it changes to Image2 and if you press again it goes back to Image1, like a loop. My issue is on how to write the script for this.

\$\endgroup\$
2
  • \$\begingroup\$ It's not really clear from your question what your issue is exactly. Is it creating that UI? Where and how to store the list of images? Changing the background image of a button? Changing the texture of an object (sprite or mesh)? Or persisting the player's selection between scenes (there are other ways than PlayerPrefs to do that, by the way)? A question on this website should only ask one thing at a time, so please clarify. \$\endgroup\$
    – Philipp
    Commented Oct 31, 2018 at 13:44
  • \$\begingroup\$ Hi Philipp, sorry about that. Basically, I'm trying to create a button within the settings menu that allows you to change the image of a GameObject per click. So, let's say there are only 2 different images available to choose from and it starts with Image1, if you press the button it changes to Image2 and if you press again it goes back to Image1, like a loop. My issue is on how to write the script for this. \$\endgroup\$
    – bakashinji
    Commented Oct 31, 2018 at 14:19

1 Answer 1

1
\$\begingroup\$

In order to do this, you will need a script which has:

  • A reference to the game object which is controlled by the button
  • A list of possible sprites to cycle through
  • A counter which keeps track of which sprite in the cycle you are currently displaying
  • A method NextSprite() which is supposed to handle button click events. This method should increases the counter by one, set it back to 0 when it reached the end of the image list and then switche out the image according to the current value of the counter.

Such a script could look like this:

using UnityEngine;

public class SpritePicker : MonoBehaviour {

    public GameObject controlledObject;
    public Sprite[] sprites;
    private int counter;

    public void NextSprite() {
        counter++;
        if (counter >= sprites.Length) counter = 0;
        SpriteRendere spriteRenderer = controlledObject.GetComponent<SpriteRenderer>();
        spriteRenderer.sprite = sprites[counter];
    }

}

Now you just need to put that script on an appropriate game object and assign a few things in the inspector:

  • Assign the game object you want to control to the "Controlled Object" slot of the Texture Picker
  • Assign the sprite assets you want to cycle through to the Sprites array of the Texture Picker
  • Create a new entry in the "On Click ()" event list of your button, assign the object with the texture picker to the entry and pick the "TexturePicker.NextImage" method for that entry.
\$\endgroup\$
9
  • \$\begingroup\$ Sorry I submitted the comment before finishing typing. Thanks for the script, it does make sense to me but I might be doing something wrong as it's not working. This is what I have done: - I added the script to the GameObject whose Image I want to change; - Dragged the same GameObject to the controlledObject slot; - Assigned different sprites to the different texture slots; - On the OnClick of a button I dragged the GameObject to it and selected the SpritePicker.NextSprite function. \$\endgroup\$
    – bakashinji
    Commented Oct 31, 2018 at 16:08
  • \$\begingroup\$ @bakashinji And then? What happens when you click the button? Any error message in the console? \$\endgroup\$
    – Philipp
    Commented Oct 31, 2018 at 16:09
  • \$\begingroup\$ It says: "MissingComponentException: There is no 'SpriteRenderer' attached to the "Background Image" game object, but a script is trying to access it. You probably need to add a SpriteRenderer to the game object "Background Image". Or your script needs to check if the component is attached before using it." I have added the Sprite Renderer to the gameObject since but it still does not work. \$\endgroup\$
    – bakashinji
    Commented Oct 31, 2018 at 16:15
  • \$\begingroup\$ @bakashinji The "Controlled Object" needs a sprite renderer component, obviously. A game object can't be represented by a sprite in the game when it doesn't have a sprite renderer. \$\endgroup\$
    – Philipp
    Commented Oct 31, 2018 at 16:19
  • 1
    \$\begingroup\$ @bakashinji So your controlled object isn't a game object in world space but an UI element on a canvas? In that case use controlledObject.GetComponent<Image>().sprite = sprites[counter] \$\endgroup\$
    – Philipp
    Commented Oct 31, 2018 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .