So I’m making a conversation system. It displays the text and portraits all fine. My problem is, that my int that keeps track of which line to display doesn’t reset to zero which results in the second line of text displaying first. Here’s my scripts:
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;
public Image Portrait;
public Sprite newSprite;
public Sprite[] Portraits;
void Start(){
}
void Update(){
if (dialougeActive && Input.GetKeyDown (KeyCode.Space)) {
currentLine++;
}
if (currentLine >= dialougeLines.Length) {
if (currentLine >= Portraits.Length) {
dialougeActive = false;
dBox.SetActive (false);
currentLine = 0;
}
}
Portrait.sprite = newSprite;
newSprite = Portraits [currentLine];
dText.text = dialougeLines [currentLine];
}
public void ShowDialouge(){
dialougeActive = true;
dBox.SetActive (true);
}
}
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;
public Sprite Portrait;
public Sprite[] Portraits;
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.Portraits = Portraits;
cm.newSprite = Portrait;
cm.currentLine = 0;
cm.dialougeLines = dialougeLines;
cm.ShowDialouge();
}
}
}
}
Hope you understand. Thanks in advance!