- Home /
my next button works but my back button doesn
So my button for advancing to the next image works perfectly but when I try and use a similar method for going back to the previous image it will take it back to the very first image. THEN my next button will just advance to what would have been the following image IE
next button goes to image 1 then 2 then 3, press the back button and it goes back to image 0 press the next button again and it will pick up at image 4, etc, what am i missing?
below in order are my canvas manager (tutorial_manager) my next_button script for advancing the image and my back_button script for going to the previous image.
public class Tutorial_Manager : MonoBehaviour {
public Sprite[] tutImage;
public Image tutorialImageDisplay;
public GameObject Next;
public GameObject Play;
public void UpdateImage (int currentImage)
{
tutorialImageDisplay.sprite = tutImage[currentImage];
if (currentImage != 11)
{
Next.SetActive(true);
Play.SetActive(false);
}
else
{
Next.SetActive(false);
Play.SetActive(true);
}
}
}
public class Tut_Next_button : MonoBehaviour {
public static Tut_Next_button next;
private Tutorial_Manager _tutorialManager;
public int image = 0;
// Use this for initialization
void Start ()
{
_tutorialManager = GameObject.Find("Canvas").GetComponent<Tutorial_Manager>();
}
// Update is called once per frame
public void GoToNextImage()
{
_tutorialManager.UpdateImage(image);
if (image < 11)
{
image++;
}
}
}
public class Back_Tut_Button : MonoBehaviour {
public static Back_Tut_Button back;
private Tutorial_Manager _tutorialManager;
public int image;
public void Start()
{
_tutorialManager = GameObject.Find("Canvas").GetComponent<Tutorial_Manager>();
}
public void Update ()
{
image = Tut_Next_button.next.image;
}
public void GoToPreviousImage()
{
_tutorialManager.UpdateImage(image);
if (image > 0)
{
image--;
}
}
}
Answer by Vega4Life · Dec 16, 2018 at 09:53 PM
Just put the next and previous methods inside your Tutorial_Manager and have your buttons set to those. Then the Manager can control the image count. Full script below. Just update your buttons method references.
public class Tutorial_Manager : MonoBehaviour
{
public int image = 0; // ADD THIS
public Sprite[] tutImage;
public Image tutorialImageDisplay;
public GameObject Next;
public GameObject Play;
// Dont need parameters - just use image count
void UpdateImage()
{
tutorialImageDisplay.sprite = tutImage[image];
if (image != 11)
{
Next.SetActive(true);
Play.SetActive(false);
}
else
{
Next.SetActive(false);
Play.SetActive(true);
}
}
// ADD THESE
public void GoToNextImage()
{
UpdateImage();
if (image < 11)
{
image++;
}
}
public void GoToPreviousImage()
{
UpdateImage();
if (image > 0)
{
image--;
}
}
}
if that's all it is I feel really silly for having sooooooooo many scripts on simple gameobjects throughout my game as a whole. will test it soon as I get home. thanks as always Vega!
I think it's okay to have a lot of scripts doing things, but in this case, it isn't the buttons job to deter$$anonymous$$e which image to use or keep track of. Their job is to let something else know they were pressed. They should let other scripts deter$$anonymous$$e what to do after that.
so it's sort of working
lets say i've gotten to image 3, but i now want to go back
when i press the back button it will go to image 4 but if i press it again it will then go back to 3 then 2 etc. basically the same thing happens when i've been going backwards but now want to go forwards.
also, i don't know if they're related, but when i'm on the first image, when i first press the next button it doesn't do anything, i have to press it a second time for the images to begin advancing.
public class Tutorial_$$anonymous$$anager : $$anonymous$$onoBehaviour {
public int image = 0;
public Sprite[] tutImage;
public Image tutorialImageDisplay;
public GameObject Next;
public GameObject Play;
public void UpdateImage ()
{
tutorialImageDisplay.sprite = tutImage[image];
Debug.Log("image " + image);
if (image != 12)
{
Next.SetActive(true);
Play.SetActive(false);
}
else
{
Next.SetActive(false);
Play.SetActive(true);
}
}
public void GoToNextImage()
{
UpdateImage();
if (image < 12)
{
image++;
}
}
public void GoToPreviousImage()
{
UpdateImage();
if (image > 0)
{
image--;
}
}
public void GoToNextImage()
{
if (image < 12)
{
image++;
}
UpdateImage();
}
public void GoToPreviousImage()
{
if (image > 0)
{
image--;
}
UpdateImage();
}
do UpdateImage after you decrement or increment
i always forget about the hierarchy component of coding. It's crazy how something as simple as which line is first will cause something to work so much more smoothly. THAN$$anonymous$$S!
Your answer
Follow this Question
Related Questions
Creating a next and previous Tutorial 1 Answer
Unity UI How to make Multiple Clickable Next / Back Buttons? 3 Answers
Side battle tutorial using 3d objects 0 Answers
Enemy AI script trouble(Js to c#) 3 Answers