Make a map-selection-button

Hi,

what ist the best way to make a map-selection-button (like in the attached file)?

It should switch between three maps if you press the “left” or “right” arrow on the keyboard.
Also the button should only work if it is selected. Should I use an UIButton so that I can select the gameObject?

Greetings
J.

3504818--279563--mapselection.png

i would have an IF statement to check an int or check an enum. and then have the buttons press left would subtract the int. and press right would add to the int. every time the a button is press, updated the image based on the int value.
I would use UI buttons

it would look something like this. and would be attached to the map gameObject

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MapManager : MonoBehaviour
{
    int selectedMap = 0;
    public Sprite[] maps;
    private Sprite mySprite;

    void Start()
    {
        mySprite = GetComponent<Sprite>();
        if (!maps)
        {
            Debug.Log("ERROR - Maps was not setup in the inspector");
        }
        UpdateMap();
    }

    public void PressLeft()
    {
        if (selectedMap == 0)
        {
            selectedMap = maps.Length - 1;
        }
        else
        {
            selectedMap--;
        }
        UpdateMap();
    }

    public void PressRight()
    {
        if (selectedMap == maps.Length -1)
        {
            selectedMap = 0;
        }
        else
        {
            selectedMap++;
        }
        UpdateMap();
    }

    private void UpdateMap()
    {
        mySprite.sprite = maps[selectedMap];
    }
}

Edit: code was not tested, just free handed

opps, i forgot about the map name box that’s in between the arrows.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MapManager : MonoBehaviour
{
    int selectedMap = 0;
    public Sprite[] maps;
    public string[] mapNames;
    private Sprite mySprite;
    public Text mapBtnText;

    void Start()
    {
        mySprite = GetComponent<Sprite>();
        if (!maps)
        {
            Debug.Log("ERROR - Maps was not setup in the inspector");
        }
        if(!mapBtnText)
        {
            Debug.Log("ERROR - mapBtnText was not setup in the inspector");
        }
        UpdateMap();
    }

    public void PressLeft()
    {
        if (selectedMap == 0)
        {
            selectedMap = maps.Length - 1;
        }
        else
        {
            selectedMap--;
        }
        UpdateMap();
    }

    public void PressRight()
    {
        if (selectedMap == maps.Length -1)
        {
            selectedMap = 0;
        }
        else
        {
            selectedMap++;
        }
        UpdateMap();
    }

    private void UpdateMap()
    {
        mySprite.sprite = maps[selectedMap];
        mapBtnText.text = mapNames[selectedMap];
    }
}

Thank you, that’s great! At the moment I have problems with the selectable object, it has a “Selectable” Script on it, and in my own Script I do this:

 if(Selection.activeObject == mapObject){
                Debug.Log("selected");
}

but nothing happens if I have it selected (I can see that it is selected because it changes the color). What did I wrong?

not enough code.
this code only checks if it’s selected. So i have no idea how you are selecting the object

are you doing this on the clickable object?
void OnMouseDown()
{
Selection.activeObject = gameObject;
}

This is what I want, I need this peace of code so that I can check if the player presses left or right only if the object is selected. But if I select it with my keyboard (its not clickable, only selectable with controller/keyboard) the console doesn’t show the Debug.Log(“selected”).

ummm, I thought you where using a UI button.
Look up OnClick() events. and then use the code i provided.

Why does it need to be selected first?

I changed it a little bit… but I made a mistake, I thought “Selection.activeObject = gameObject;” is for the currently selected object in the event system, but it isn’t. Instead I used “eventSystem.currentlySelectedGameObject”. Now I don’t use a button, I am checking if the object is selected and if the player presses left or right, then it switches through the maps in this direction. But thank you for the help!