Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Reading from and writing to Text in Button

Discussion in 'Scripting' started by unity_hp0JJK1sjqD8Jw, Aug 2, 2018.

  1. unity_hp0JJK1sjqD8Jw

    unity_hp0JJK1sjqD8Jw

    Joined:
    Aug 2, 2018
    Posts:
    1
    //edit: after posting, I realized my title is not accurate - I'm sorry about that.


    Dear Unity community,


    I started with Unity and C# a few months back as part of a class project in my university.

    I wanted to create a tutorial scene the user can easily navigate through.
    Therefore I simply created two buttons for "previous" and "next" pages as well as a textfield displaying "Page 1/5"


    About the code:
    In my button controller script, I set the "page" variable to 1 in my Start() function, as that's always the first page displayed.
    After a button is pressed, my "page" int is either increased or decreased.
    If the page number hits an end (Page 1/5 or Page 5/5) the console displays "first/last page reached"


    My problem:
    After testing my code, the "next" button worked as intended, always increasing the page by 1, and the correct console print at page 5.

    Whenever I click on the previous button though, the console tells me "first page reached".
    This occurs on whatever page I am currently on.


    I narrowed my problem down to the following:
    • If the script starts, the page int is always set to 1.
    • I can not decrease the value at first, as it is already one, but I can increase it.
    • As soon as I switch from the "next" button to the "previous" button, the Start() function sets my page counter back to 1, thus enabling the console print "first page reached".
    So my problem is basically that if i press "next" multiple times, the Start() function only gets called on the first press. If I press the "previous" button afterwards, the script gets called again and sets back my page counter.


    Is there a simple solution here I'm not seeing?
    I would be happy already if someone could give me a few hints so I can figure out the solution by myself!
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I assume these pages are in the same scene. If you are using a page per scene, then variables can get lost / reset that way. Incidentally, I'm not suggesting you use scenes here, just making the point.

    So if this is same scene, the other way that variables can (appear to) be reset, is if the GameObject on which they reside is destroyed and then created anew.

    It sounds like you might want to create something like a page manager class. That would keep track of what page you are on and could maybe control the text that the buttons should display.

    Does that help out at all?