Any way to update a string array mid game?,Update the strings in a string array mid game?
I have a couple of dialogue scripts set up to print out text from an array, but the text doesn't update... it keeps trying to read my inactive array instead of the active one...
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class TextDraw : MonoBehaviour
{
private DialougeManager dMan;
public Text TextBeingEdited;
public float Delay = 0.1f;
private DialougeHolder dHold;
public int AmountofLines;
private int currentLine = 0;
private string CurrentText = "";
public string[] textBeingDrawn;
public bool DialougeActive;
public GameObject DialougeManager;
private bool isDrawing;
AudioSource SpeechSound;
public bool is_menu;
public bool ForStory;
public bool IsActive;
// Use this for initialization
void Awake()
{
Debug.Log("Active Now");
if (IsActive == true)
{
isDrawing = true;
DialougeManager.SetActive(true);
if (currentLine >= AmountofLines)
{
currentLine = 0;
}
CurrentText = textBeingDrawn[currentLine];
DialougeActive = true;
SpeechSound = GetComponent<AudioSource>();
StartCoroutine(DrawText());
}
}
void Update()
{
if(ForStory == true)
{
if(currentLine >= AmountofLines)
{
currentLine = 0;
DialougeActive = false;
DialougeManager.SetActive(false);
}
}
if(is_menu == true)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
DialougeActive = false;
}
}
if (is_menu == true)
{
if (Input.anyKeyDown && DialougeActive == true && isDrawing == false)
{
currentLine++;
isDrawing = true;
StartCoroutine(DrawText());
}
}
else
{
if (Input.GetKeyDown(KeyCode.Space) && DialougeActive == true && isDrawing == false)
{
currentLine++;
isDrawing = true;
StartCoroutine(DrawText());
}
}
if (currentLine >= AmountofLines)
{
DialougeActive = false;
currentLine = 0;
DialougeManager.SetActive(false);
}
CurrentText = textBeingDrawn[currentLine];
}
IEnumerator DrawText()
{
if(isDrawing == true)
{
for (int i = 0; i < CurrentText.Length; i++)
{
CurrentText = CurrentText.Substring(0, i);
SpeechSound.Play();
TextBeingEdited.text = CurrentText;
yield return new WaitForSeconds(Delay);
}
isDrawing = false;
}
else
{
isDrawing = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialougeTrigger : MonoBehaviour {
public GameObject TextToTrigger;
private TextDraw TD;
void Start()
{
TD = TextToTrigger.GetComponent<TextDraw>();
}
// Use this for initialization
void OnTriggerEnter2D (Collider2D other) {
if(other.transform.tag == "Player")
{
Debug.Log("Entered");
TextToTrigger.SetActive(true);
TD.DialougeActive = true;
TD.IsActive = true;
}
}
// Update is called once per frame
void Update () {
}
}
I know the script is being read because both debug.log statements show up in the console and enabling the old array makes it read that off.
Any way to update the array after one script closes? Thanks!
it is hard to know without any comments or anything, but what you are trying to achieve, what do you mean is trying to read your inactive array? you only have 1 array. Your problem is the string being shown in the text is always the same? or whats the exact problem?
Answer by SinglePlayerPlus · Jan 23, 2019 at 08:59 PM
I have the same script on two different objects. When one is disabled. I want the array to clear, so the next one that loads array is the one that shows.
But can you open both dialogues at the same time? since you can trigger the second while still be reason the first right?
Just tried, if they both start enabled I can trigger both at the same time, but if I trigger one later it doesn't work.
each script has its own text? or they share it? sorry im having issues to imagine how it was implemented.