Hello! Thank you for reading this post. I’ve recently started a project were I’m making a game for kids with my accomplice. The point of the game is that a country and its name is supposed to light up in an orange color when that country is picked through a script that spits out a random number.
The point is then to match the name of country by choosing between three flags on the left. These three will be randomzied, then one will be replaced with the correct answer. How we’ve done it up to this point is that we’ve used the UI element RawImage then used flags which we’ve painted as the texure. Now to my real question, is it possible in anyway to manipulate what texture the RawImage has through scripting? Any suggestions or anwsers are accepted, or if you have a better idea of how to create the system please leave a comment.
Several ways to set this up.
Here is one:
Create a class (say, ImageClass) that exposes properties for your RawImage and Textures. You can use arrays for the textures, create your own custom editor property, etc.
Quick and dirty just to show how to change the texture…your code would need to handle the swap, presumably based on game logic:
The texture property of the RawImage gets/sets the Texture (different from Image, which gets/sets Sprite)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ImageClass : MonoBehaviour {
public GameObject ImageOnPanel; ///set this in the inspector
public Texture NewTexture;
private RawImage img;
void Start () {
img = (RawImage)ImageOnPanel.GetComponent<RawImage>();
img.texture = (Texture)NewTexture;
}
// Update is called once per frame
void Update () {
}
}