Raw Image changing texture in inspector but not in game.
Hey, there, I'm working on a dialogue system for my game and I wanted to have a picture of the character who is speaking in the corner next to the text. The dialogue system is working pretty much perfectly but for some reason the image of the character does not change. I can see it changing in the inspector but in game nothing changes. I've been toying around with it for hours now but I'm not great at coding and can't seem to figure it out. I'll put the entire script below and any help with this would be greatly appreciated.
public TextMeshProUGUI textDisplay;
public string[] sentences;
public RawImage imageSpace;
public Texture2D[] CharacterPortraits;
private int CurrentCharTexture;
private int index;
public float typingSpeed;
public GameObject ContinueButton;
public DialogueBegin DialogueBegin;
void Start()
{
StartCoroutine(Type());
Cursor.visible = true;
Screen.lockCursor = false;
imageSpace.GetComponent<RawImage>().material.mainTexture = CharacterPortraits[0];
index = 0;
}
private void Update()
{
if (textDisplay.text == sentences[index])
{
ContinueButton.SetActive(true);
}
}
IEnumerator Type()
{
foreach (char letter in sentences[index].ToCharArray())
{
textDisplay.text += letter;
yield return new WaitForSeconds(typingSpeed);
}
}
public void NextSentence()
{
ContinueButton.SetActive(false);
if (index < sentences.Length - 1)
{
//text
index++;
textDisplay.text = "";
StartCoroutine(Type());
//images
CharImageChange();
}
else
{
textDisplay.text = "";
ContinueButton.SetActive(false);
DialogueBegin.End();
}
}
private void CharImageChange()
{
CurrentCharTexture++;
imageSpace.GetComponent<RawImage>().material.mainTexture = CharacterPortraits[CurrentCharTexture];
}
Answer by MrSentient · Apr 06, 2020 at 04:07 PM
For anyone interested, I figured it out myself. Instead of using:
ImageSpace.GetComponent().material.mainTexture = textures[currentTexture];
use:
imageSpace.texture = textures[currentTexture];
that seemed to fix the problem.
Your answer
Follow this Question
Related Questions
Problem with menu 1 Answer
How can i make a tiled/striped texture on a sphere ? (see example below) 0 Answers
How can I set an Image (PNG) from a URL to be the material of a sphere ?? 0 Answers
What right way to SetTextureOffset of material which has dynamic count owners? 0 Answers
Lightweight RP change texture scale 0 Answers