- Home /
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
Click 2
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();
}
}
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.
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
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.
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! :)
Your answer
Follow this Question
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