Change UI image in a conversation
Hey there!
I'm making a 2d RPG and I've just finished getting a conversation system to display properly. So the text and all displays when I want it to and it's all good. However, I'd like the character's face to appear beside my text but I don't know how I'd do that. I've tried to have an empty image and change it in script but nothing is working. Please help :)
My conversation script that goes on the Canvas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ConversationManager : MonoBehaviour {
public GameObject dBox;
public Text dText;
public bool dialougeActive;
public string[] dialougeLines;
public int currentLine;
void Start(){
}
void Update(){
if (dialougeActive && Input.GetKeyDown (KeyCode.Space)) {
currentLine++;
}
if (currentLine >= dialougeLines.Length) {
dBox.SetActive (false);
dialougeActive = false;
currentLine = 0;
}
dText.text = dialougeLines [currentLine];
}
public void ShowDialouge(){
dialougeActive = true;
dBox.SetActive (true);
}
}
Other script that goes on the NPC:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class NPConversation : MonoBehaviour { public string dialouge;
public string[] dialougeLines;
private ConversationManager cm;
// Use this for initialization void Start () {
cm = FindObjectOfType<ConversationManager> ();
} // Update is called once per frame void Update () {
} void OnCollisionStay2D(Collision2D other){ if(other.gameObject.tag == "Player" && Input.GetKey(KeyCode.Space)){
if (!cm.dialougeActive) {
cm.dialougeLines = dialougeLines;
cm.currentLine = 0;
cm.ShowDialouge();
}
} } }
Comment
Best Answer
Answer by Zoogyburger · May 26, 2017 at 06:27 PM
Why don't you try doing the same things with handling the text as the images also? So, something like:
public GameObject dBox;
public Image Portrait;
public bool dialougeActive;
public Sprite newSprite;
public Sprite[] Portraits;
public int currentLine;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (dialougeActive && Input.GetKeyDown (KeyCode.Space)) {
currentLine++;
}
if (currentLine >= Portraits.Length) {
dialougeActive = false;
dBox.SetActive (false);
currentLine = 0;
}
//Image img = GetComponent<Image>();
Portrait.sprite = newSprite;
newSprite = Portraits [currentLine];
}
public void ShowDialouge(){
dialougeActive = true;
dBox.SetActive (true);
}
}