Next and Back Button?
I'm trying to create a couple buttons; A "Next" button to go forward through an array of objects and a "Previous" button to go back to the previous object in the array. Right now i have the next button working with this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NextSceneScript : MonoBehaviour {
public GameObject[] characterScenes;
private int currentCharacterScene;
public void NextScene(){
characterScenes [currentCharacterScene].SetActive (false);
if (currentCharacterScene >= characterScenes.Length - 1) {
currentCharacterScene = 0;
characterScenes [currentCharacterScene].SetActive (true);
//Debug.Log ("Function Part 1");
} else {
currentCharacterScene++;
characterScenes [currentCharacterScene].SetActive (true);
//Debug.Log ("Function Part 2 array lenght = " + characterModels.Length.ToString());
}
}
// Use this for initialization
void Start () {
for (int i = 0; i < characterScenes.Length; i++) {
characterScenes [i].SetActive (false);
currentCharacterScene = 0;
characterScenes [0].SetActive (true);
}
}
}
So basically I need to create the inverse with another script.
Thanks in advance.
Answer by polan31 · May 21, 2020 at 09:05 PM
I know we already have 2020, but after searching for a problem in google, your post is the first, so I put a script that solves the problem. Maybe for someone will be useful :) This script also allows you to hide the left arrow on the first object from the list and hide right arrow on the last object on the list.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using UnityEngine.SceneManagement;
public class MenuPanel : MonoBehaviour
{
public GameObject[] tutorialPage;
[SerializeField] GameObject buttonLeft;
[SerializeField] GameObject buttonRight;
int currentIndex = 0;
public int CurrentIndex
{
get
{
return currentIndex;
}
set
{
if (tutorialPage[currentIndex] != null)
{
//set the current active object to inactive, before replacing it
GameObject aktivesObj = tutorialPage[currentIndex];
aktivesObj.SetActive(false);
}
if (value < 0)
currentIndex = tutorialPage.Length - 1;
else if (value > tutorialPage.Length - 1)
currentIndex = 0;
else
currentIndex = value;
if (tutorialPage[currentIndex] != null)
{
GameObject aktivesObj = tutorialPage[currentIndex];
aktivesObj.SetActive(true);
}
}
}
public void Previous(int direction)
{
if (direction == 0)
CurrentIndex--;
if (CurrentIndex <= 3)
{
buttonRight.SetActive(true);
Debug.Log("4");
}
if (CurrentIndex <= 0)
{
buttonLeft.SetActive(false);
Debug.Log("3");
}
}
public void Next(int direction)
{
if (direction >= 1)
CurrentIndex++;
if (CurrentIndex >= 4)
{
buttonRight.SetActive(false);
Debug.Log("2");
}
if (CurrentIndex >= 1)
{
buttonLeft.SetActive(true);
Debug.Log("3");
}
}
public void OnClickTutorialPanel()
{
if (CurrentIndex <= 0)
{
buttonLeft.SetActive(false);
buttonRight.SetActive(true);
Debug.Log("1");
}
tutorialPage[0].SetActive(true);
Tutorial.SetActive(true);
}
public void OnClickBackToSettings(){
CurrentIndex = 0;
tutorialPage[currentIndex].SetActive(false);
Tutorial.SetActive(false);
}
}
Hey polan31,
Can you explain how to set up the script on 2D UI ?
I would like to use it as an image change in my scene.
Thanks for charging
Your answer
Follow this Question
Related Questions
ui button function and onclick event 1 Answer
Change color for disabled button 1 Answer
Solved: Cant get onclick assignet to prefabs button 1 Answer
Array.Sort() - JS only???! 0 Answers
Need help with Mute Button 1 Answer