Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by surfuay · Dec 16, 2018 at 08:35 PM · imagetutorialbacknext

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--;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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--;
         }
     }
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image surfuay · Dec 16, 2018 at 09:58 PM 0
Share

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!

avatar image Vega4Life surfuay · Dec 16, 2018 at 10:29 PM 0
Share

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.

avatar image surfuay · Dec 16, 2018 at 11:40 PM 0
Share

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--;
     }
 }
avatar image Vega4Life surfuay · Dec 17, 2018 at 01:21 AM 0
Share
  public void GoToNextImage()
  {
 
      if (image < 12)
      {
          image++;
      }
 
      UpdateImage();
  }
 
  public void GoToPreviousImage()
  {
      if (image > 0)
      {
          image--;
      }
 
      UpdateImage();
  }



do UpdateImage after you decrement or increment


avatar image surfuay · Dec 17, 2018 at 07:45 PM 0
Share

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!

avatar image Vega4Life surfuay · Dec 17, 2018 at 07:58 PM 0
Share

You're welcome. Good luck!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity UI How to make Multiple Clickable Next / Back Buttons? 3 Answers

Creating a next and previous Tutorial 1 Answer

Common way to make a GameTutorial? 1 Answer

2D Game example 2 Answers

Best way to make a bullet hell shooter in Unity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges