Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 watchermagic3 · Sep 08, 2018 at 12:49 AM · buttonarrayobjectloaddisplay

Displaying object with button

[Edited; thank you YBtheS!]

I have a scriptable object called a "page" that contains both text and an array of "next pages." I'm trying to get this script so that when I click a button it loads the corresponding page in the array, and I can't seem to get it to display. Here's what happens when I click the button:

Click 1 alt text

Click 2 alt text

So apparently the next page is loaded but not displayed? If anyone can help me figure out what to do it would be amazing

 public class UserInputHandler : MonoBehaviour
 {
     AdventureGame adventureGame;
     [SerializeField] Button a;
     [SerializeField] Button b;
     [SerializeField] Button c;
     Page currentPage;
     Page[] nextPages;
 
     bool pageNamed;
 
     //Start Menu
     public void NewStoryButton()
     {
         //prompt for story name
         //create folder for story pages titled with the story name
         //load new page in editor
     }
 
     //Editor
     void Start ()
     {
         adventureGame = FindObjectOfType<AdventureGame>();
     }
     
     private void PageButtons()
     {
         Page p = adventureGame.GetCurrentPage();
         nextPages = new Page[p.GetNextPages().Length];
         Debug.Log(nextPages.Length);
         nextPages = p.GetNextPages();
     }
 
 
     //A: if adventureGame currentPage has nextPages[0], load nextPages[0], else create new page
     public void A_Button()
     {
         PageButtons();
 
         //if currentPage has nextPage[0] = true, load nextPages[0]
         if (nextPages[0] == null)
         {
             adventureGame.CreateNewPage();
             Debug.Log("New page created");
         }
 
         else
         { //these next two lines are what I've tried so far
             adventureGame.SetCurrentPage(nextPages[0]);
             adventureGame.ManagePage();
             Debug.Log(nextPages[0]);
         }
 
     }
 
     //B: if currentPage has nextPages[1], return true; else false
     public void B_Button()
     {
         PageButtons();
         adventureGame.ManagePage();
 
         // if currentPage has nextPage[1] = true, load nextPages[0]
         if (nextPages[1] == null)
         {
             adventureGame.CreateNewPage();
         }
 
         else
         {
             
         }
 
     }
 
     //C: if currentPage has nextPages[2], return true; else false
     public void C_Button()
     {
         PageButtons();
         adventureGame.ManagePage();
 
         if (nextPages[2] == null)
         {
             
         }
 
         else
         {
             Debug.Log(nextPages[2]);
         }
     }
 
 
     private void DisableButtons()
     {
         PageButtons();
         
         //if nextPages[] has no elements, disable B & C
         if (nextPages[0] == null && nextPages[1] == null && nextPages[2] == null)
         {
             a.interactable = true;
             b.interactable = false;
             c.interactable = false;
         }
         //if nextPages[] does not have elements 0 & 1, disable C
         else if (nextPages[1] == null && nextPages[2] == null)
         {
             a.interactable = true;
             b.interactable = true;
             c.interactable = false;
         }
         //if nextPages[] has all 3 elements, buttons enabled
         else
         {
             a.interactable = true;
             b.interactable = true;
             c.interactable = true;
         }
     }
 
     private void Update()
     {
         DisableButtons();
     }
 
 }

public class AdventureGame : MonoBehaviour { [SerializeField] public TextMeshProUGUI storyText; [SerializeField] Page newPage;

 public Page currentPage;
 Page[] nextPages;

 // Use this for initialization
 void Start()
 {
     CreateNewPage();
 }

 public void CreateNewPage()
 {
     currentPage = newPage;
     GetPageText();

 }

 public Page GetCurrentPage()
 {
     return currentPage;
 }

 public void SetCurrentPage(Page page)
 {
     currentPage = page;
 }

 public void ManagePage()
 {
     nextPages = currentPage.GetNextPages();
     GetPageText();
 }

 private void GetPageText()
 {
     storyText.text = currentPage.GetPageText();
 }

}

screenshot-801.png (120.0 kB)
screenshot-800.png (116.2 kB)
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
0

Answer by YBtheS · Sep 08, 2018 at 02:45 AM

You never initialized the array nextPages. Add a new line after line 18 and write newPages = new Page[p.GetNextPages.Length];. I believe that should work.

Comment
Add comment · Show 4 · 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 watchermagic3 · Sep 08, 2018 at 04:38 PM 0
Share

I put in the line

 nextPages = new Page[p.GetNextPages().Length];

in the middle of PageButtons(), however when I try and click the button I still get an error Array Index is Out of Range on line 46

avatar image YBtheS watchermagic3 · Sep 09, 2018 at 08:46 PM 0
Share

Hmm... try doing Debug.Log(nextPages.Length); on the line after that. What does it print? If the number is less than three then that's that problem.

avatar image watchermagic3 YBtheS · Sep 11, 2018 at 08:22 PM 1
Share

Ah! Thank you, that was just what I needed. I was really confused because it printed 3 first, and then 0 with the error; then I realized that I was updating to a new object that had an array length of 0 without loading it onscreen. I changed the array length and it's fixed, thank you for the help! :)

Show more comments

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

104 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 avatar image avatar image

Related Questions

Loading a Model 1 Answer

Saving/Loading variables 1 Answer

Cannot access class array in dynamically created object 1 Answer

how to load images from hard disk to gui buttons? 1 Answer

Resources Load array 0 Answers


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