- Home /
Quiz Game: How to replace Text with Images?
So, I followed an official Unity tutorial about making a Quiz Game. I successfully followed each and every step and it works great. Now I want to replace the text on the buttons with images. I tried dragging one of the images into the button UI but it ends up changing all of it instead of just one single button. So how do I replace the text with an image on a button?
TUTORIAL LINK: http://www.unity3d.com/learn/tutorials/topics/scripting/quiz-game-intro-and-setup
Answer by rrabi · Aug 13, 2017 at 12:23 AM
@bryanllanes its because there is probably one or few button elements used to create all the choices, so buttons text is being changed at the runtime through code. look in the code where he sets the text of the buttons. when you find. instead of text start replacing with the images you want to set. if you need help with changing the image through code let me know.
I need help with changing the image through code please.....I have searched for clues for days and still unable to find it....It would be really helpful if you could write the code or give out hints on applying it throughout the script
okay i looked through the scripts in the tutorial. i will give you a hint. in your AnswerData class change
public string answerText;
to
public Sprite answer
in your AnswerButton class. Change
public Text answerText;
to
public Image answer;
now change the Setup function to
public void Setup(AnswerData data)
{
answerData = data;
answer.sprite = answerData.answer;
}
now your image changing code will work. you will receive some errors after these changes which will help you that where else do you need the change the text types to images/sprites.
.....it looks very simple.....Thank you, I will try it out
I follow all you say but the button is not changing. I even change the text to image but when i run i always see is a single button without an image that i put. Plsss answer what the other that i need to change?
Sir we have the same problem. What did you do? can't find any clue from the internet since i'm beginner in program$$anonymous$$g.
Answer by anthonygonzalez · Aug 17, 2017 at 03:22 AM
When I change it to factText.sprite, currentQuestion.vraag gives an error that says “Cannot implicitly convert type ‘UnityEngine.UI.Image’ to ‘UnityEngine.sprite’.”
[System.Serializable]
public class Questions {
public string fact;
public string answerOne;
public string answerTwo;
public string answerThree;
public string answerFour;
public bool isOne;
public bool isTwo;
public bool isThree;
public bool isFour;
}
This is the one that’s used for the text one.
using UnityEngine.UI;
[System.Serializable]
public class ImageVragen
{
public Image vraag;
public Image antwoord1;
public Image antwoord2;
public Image antwoord3;
public Image antwoord4;
public bool isOne;
public bool isTwo;
public bool isThree;
public bool isFour;
}
And this is the one that is used for the image one
(also thank you for trying to help me so far, much appreciated)