0

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.

Philipp
  • 119,250
  • 27
  • 256
  • 336
bakashinji
  • 15
  • 6
  • 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. – Philipp Oct 31 '18 at 13:44
  • 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. – bakashinji Oct 31 '18 at 14:19

1 Answers1

1

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.
Philipp
  • 119,250
  • 27
  • 256
  • 336
  • 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.
    – bakashinji Oct 31 '18 at 16:08
  • @bakashinji And then? What happens when you click the button? Any error message in the console? – Philipp Oct 31 '18 at 16:09
  • 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. – bakashinji Oct 31 '18 at 16:15
  • @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. – Philipp Oct 31 '18 at 16:19
  • That fixes the error but the images still don't appear on the object.. – bakashinji Oct 31 '18 at 16:25
  • @bakashinji I set up a test project and it works perfectly here, so it's hard to tell what your mistake could be. Does the object appear if you assign a sprite to it manually in the inspector? If it does not, then you might want to get that working first. – Philipp Oct 31 '18 at 16:56
  • Oh, no it doesn't. It works fine with the Image (Script) component but not with an assigned Sprite to the Sprite Renderer. – bakashinji Oct 31 '18 at 17:10
  • 1
    @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] – Philipp Oct 31 '18 at 17:18
  • @Phillip Yes! It's working now! Thank you for all your help :) – bakashinji Oct 31 '18 at 17:26