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?
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
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];
}
}
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”).
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!