- Home /
I need help making an image change when I press next
Okay so I have a bestiary page in my game and I made an Entry to for a string (description) as well as a UISprite (using NGUI) for a picture of the beast. I scripted a next button to call a method that cycles through the entry and changes the image and string however I cannot get the image to work. It just stays on the first image. Here's the code:
public class Bestiary : MonoBehaviour { public int Page = 0;
public UILabel EnemyTitleLabel,
EnemyDescriptionTopLabel,
EnemyDescriptionBottomLabel;
public UISprite EnemyImage;
public Entry[] Entries = new Entry[12];
[System.Serializable]
public class Entry
{
public string Title = string.Empty;
public string Description = string.Empty;
public UISprite Image;
public Entry(string title, string description, UISprite image)
{
Title = title;
Description = description;
Image = image;
}
}
void Awake()
{
EnemyImage = GameObject.Find("Enemy Image").GetComponent<UISprite>();
EnemyTitleLabel = GameObject.Find("Enemy Title").GetComponent<UILabel>();
EnemyDescriptionTopLabel = GameObject.Find("Enemy Description Top").GetComponent<UILabel>();
EnemyDescriptionBottomLabel = GameObject.Find("Enemy Description Bottom").GetComponent<UILabel>();
EnemyTitleLabel.text = Entries[0].Title;
AssignText(Entries[0].Description);
EnemyImage = Entries[0].Image;
}
private const int TopLabelCharLimt = 200;
private void AssignText(string text)
{
if (text.Length > 200)
{
string TopText = text.Substring(0, TopLabelCharLimt);
string BottomText = text.Substring(TopLabelCharLimt, text.Length);
EnemyDescriptionTopLabel.text = TopText;
EnemyDescriptionBottomLabel.text = BottomText;
}
else
{
EnemyDescriptionTopLabel.text = text;
}
}
public void NextEntry()
{
if (Page + 1 == Entries.Length)
{
Page = 0;
EnemyTitleLabel.text = Entries[Page].Title;
AssignText(Entries[Page].Description);
EnemyImage = Entries[Page].Image;
}
else
{
Page++;
EnemyTitleLabel.text = Entries[Page].Title;
AssignText(Entries[Page].Description);
EnemyImage = Entries[Page].Image;
}
}
public void PreviousEntry()
{
if (Page - 1 == -1)
{
Page = 11;
EnemyTitleLabel.text = Entries[Page].Title;
AssignText(Entries[Page].Description);
EnemyImage = Entries[Page].Image;
}
else
{
Page--;
EnemyTitleLabel.text = Entries[Page].Title;
AssignText(Entries[Page].Description);
EnemyImage = Entries[Page].Image;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613143518im_/https://answers.unity.com/themes/thub/images/avi.jpg)