Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gomuelson · Dec 11, 2012 at 02:08 PM · gameobjectarrayguitextenable

When object i in Array is enabled, all other objects in Array are disabled.

I have an interesting problem that I'm probably overthinking, so I could use a fresh perspective. My game has a journal, each "page" of the journal is a GUIText with info about a different animal (on this level, it's 3 pages). When you press "open journal", it creates the "Prev" and "Next" buttons to flip through the journal.

All GUIText components are disabled initially. When you open the journal and press Next, it sets page 0 to "enabled" and increments i++ so the next time you click Next, page 1 is enabled. I tried to add "disable i-1" but since 0-1 = -1 it says Error: Index out of Range the first time I click.

Essentially, I need PageOn() to say "when object i is enabled, all other objects in the array are disabled", so you can only read one page at a time without text overlapping each other. I need this to be reusable for an array of any size, because the journal will have different numbers of pages on each level. And eventually, I want the reverse to happen when you press the "Prev" button. Thanks in advance for any help.

 var subMenu : boolean = false;
 var aAnimals : GUIText[];
 private var i : int = 0;
 
 function Start()
 {
     //turn prev and next buttons off initially
     subMenu = false;
     i = 0;
 }
 
 function OnGUI()
 {
     if(!subMenu)
     {
         if(GUI.Button(Rect(10,10,100,50), "Open Journal"))
         {
             subMenu = true;
             Debug.Log("opened journal");
         }
     }
     
     else if(subMenu)
     {
         //when journal opened, create prev and next buttons
         if(GUI.Button(Rect(10,75,50,50), "Prev"))
         {
         //Go to previous page
         }
         
         if(GUI.Button(Rect(75,75,50,50), "Next"))
         {
         //Go to next page
         PageOn();
         }

         //changes Open Journal to say Close Journal
         if(GUI.Button(new Rect(10,10,100,50), "Close Journal"))
         {
         Debug.Log("closed journal");
         //turns off prev and next buttons
         subMenu = false;
         }
     }
 }
 
 function PageOn()
 {
     //turn on page 0
     aAnimals[i].enabled = true;
     //increment i for the next time the "Next" button is pressed
     i++;
     //wrapper to prevent "index out of range" error when you reach the end
     i = i % aAnimals.Length;
 }
Comment
Add comment · Show 2
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 Kryptos · Dec 11, 2012 at 04:23 PM 0
Share

To make a cycling counter (both positive and negative) use:

 i = (i + Count) % Count; // here Count is aAnimals.Length
avatar image FlowStateGames · Dec 11, 2012 at 05:16 PM 0
Share

While that is an optimization, and there's nothing wrong with that, I'm a bit concerned about writing statements like that. For instance, say that the gui element is a 'book'. If going to the next page and going back a page 'flips the page on the book' with an animation, you definitely make it harder on yourself if you combine both types of operations.

2 Replies

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

Answer by astorga · Dec 11, 2012 at 04:41 PM

You just need to do some checks to make these operations:

First of all, make sure the "Previous" button doesn't appear when you are on first page (same check for last page and "Next" button).

Every time you advance a page, enable the next/previous one and then disable the current one (if you won't animate it, the order doesn't matter that much. Just make sure there's always a page to show). Like here:

 function OnGUI(){
     //...
     //check if we can go back one page
     if(i > 0){
         if(GUI.Button(Rect(10,75,50,50), "Prev"))
         {
             //Go to previous page
             PrevPage();
         }
     }
 
     //check if we can advance a page
     if(i < (aAnimals.Length - 1)){
         if(GUI.Button(Rect(75,75,50,50), "Next"))
         {
             //Go to next page
             NextPage();
         }
     }
 }
 
 function NextPage(){
     aAnimals[i+1].enabled = true;
     aAnimals[i].enabled = false;
     i++;
 }
 
 function PrevPage(){
     aAnimals[i-1].enabled = true;
     aAnimals[i].enabled = false;
     i--;
 }

It will work as long as you "open the book" with only one page enabled. Is it what you need?

Comment
Add comment · Show 3 · 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 gomuelson · Dec 11, 2012 at 05:38 PM 0
Share

EDIT: Added the correct code.

That worked nicely, thank you. I had to add a little to your code, like i-- for PrevPage. Also, I added some code to show page 1 when Open Journal is called, and to disable all pages when Close Journal is called. Overall it works.

I still have one error message though, when I click Next Page on the last page, I still get "Array index is out of range", even though I have the % wrapper, it doesn't seem to work. Here's the current code:

 function Start()
 {
     sub$$anonymous$$enu = false;
     i = 0;
 }
 
    function OnGUI()
     {
     if(!sub$$anonymous$$enu)
     {        
         if(GUI.Button(Rect(10,10,100,50), "Open Journal"))
         {
             sub$$anonymous$$enu = true;
             Debug.Log("opened journal");
             //show page 1 when opened
             aAnimals[i].enabled = true;
         }
     }
     
     else if(sub$$anonymous$$enu)
     {
     //check if we can go back one page
     if(i > 0)
     {
         if(GUI.Button(Rect(10,75,50,50), "Prev"))
         {
         //Go to previous page
         PrevPage();
         Debug.Log("prev page");
         }
     }
     //check if we can advance a page
     if(i < aAnimals.Length - 1)
     {
         if(GUI.Button(Rect(75,75,50,50), "Next"))
         {
             //Go to next page
             NextPage();
             Debug.Log("next page");
         }
     }
     if(GUI.Button(new Rect(10,10,100,50), "Close Journal"))
         {
         Debug.Log("closed journal");
         sub$$anonymous$$enu = false;
         //disable all pages in array when closed
         for(var i in aAnimals)
             {
                 i.enabled = false;
                     Debug.Log("disabled");
             }        
         //reset i so page 1 shows first next time
         i = 0;
         }
     }
 }
 
 function NextPage()
 {
     aAnimals[i+1].enabled = true;
     aAnimals[i].enabled = false;
     i++;
 }
 
 function PrevPage()
 {
     aAnimals[i-1].enabled = true;
     aAnimals[i].enabled = false;
     i--;
 }
avatar image astorga · Dec 11, 2012 at 05:41 PM 0
Share

Ah, my bad, sorry. The array length is 1 unit bigger than the last index. I fixed that in my answer. Take a look now.

avatar image gomuelson · Dec 11, 2012 at 05:45 PM 0
Share

That's perfect, thanks for the quick and helpful response! It turns out the wrapper is not needed anymore in NextPage() and PrevPage() so I'll edit the code to take those out.

avatar image
0

Answer by FlowStateGames · Dec 11, 2012 at 06:19 PM

Okay, so really, you are kinda overthinking this. If your initial state is that none of them are enabled, then you really only want this:

 int currentPage = 0;
 
 void NextPage()
 {
   // no matter the current page, we know that we disable
     journal[currentPage].enabled = false;
 
   // always start with the "bad case"
   if(currentPage == journal.Length - 1)
   {
     // let's loop to the first page
     currentPage = 0;
   }
   else
   {
     currentPage++;
   }
 
   
   EnablePage();
 }
 
 void PrevPage()
 {
   // no matter the current page, we know that we disable
   journal[currentPage].enabled = false;
 
   // again, start with the 'bad case'
   if(currentPage == 0)
   {
     // go to the last page
     currentPage = journal.Length - 1;
   }
   else
   {
       currentPage--;
   }
 
   EnablePage();
 }
 
 void EnablePage()
 {
   journal[currentPage].enabled = true;
 }

Now, it may look silly that I built 3 separate methods for something so trivial. But there's a reason. You could have different animations for going to a previous page, or a next page. More to the point, there could be some checks you do when you enable the page.

If you don't separate these methods, you run the real risk of creating obfuscated code when you go to add functionality. Plus, it's easier to read.

Comment
Add comment · Show 1 · 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 gomuelson · Dec 11, 2012 at 07:18 PM 0
Share

Thanks, that also works! Good learning experience :)

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

11 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

Related Questions

How to disable an array of projectors 2 Answers

how can I display a variable as a GUIText 5 Answers

Checking if a position is occupied in 2D? 1 Answer

Get all other GameObjects (not the one script is attached to) 2 Answers

Adding GameObject to GameObject[] array 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