Assigning a List string element from script A to another string variable in scriptA from scriptB
Sorry I know the title feels like inception... but I'm not sure how I could've worded it better. I've been stuck on this for a while. Heres what I'm trying to do.
I have two scripts A and B. Script A is dialogue script with a list of type string. The idea of script A is to have different string dialogues which I would write in the inspector. These elements would be assigned to another string variable in script A which should update the string. Script B is only used to call for that assignment with some kind of a condition. Heres an example of what I'm trying to do:
ScriptA: (Attached to my Text box in the hierarchy and works perfectly when tested on its own)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dialogues : MonoBehaviour
{
public static Dialogues instance;
public float delayBetweenChar = 0.1f;
public List <string> theDialogues;
public string completeText;
private string updatedText = "";
Text theText;
// Use this for initialization
void Start ()
{
theText = GetComponent<Text> ();
}
public IEnumerator ShowText()
{
for (int i = 0; i <= completeText.Length; i++)
{
updatedText = completeText.Substring (0, i);
theText.text = updatedText;
yield return new WaitForSeconds (delayBetweenChar);
}
}
}
Script B: (just trying to assign an element of theDialogues list under some condition. I'm using triggers in this one, so here's something similar to what I have). (Assigned to a character GameObject in the hierarchy)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SomeCharacter : MonoBehaviour
{
PlayerController playerController;
public GameObject myPlayer;
public float stayTime;
public int timeStay;
void Start()
{
playerController = myPlayer.GetComponent<PlayerController>();
}
void OnTriggerStay2D (Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
stayTime += Time.deltaTime;
timeStay = (int) stayTime;
switch (timeStay)
{
case 3:
//Since I have a static variable in the Dialogue script I tried using it
//directly but I'm getting a NullRef.Exception error.
Dialogues.instance.completeText = Dialogues.instance.theDialogues[1];
break;
case 5:
// do something similar to show another message from the list
break;
}
}
}
}
Answer by Bunny83 · Jan 10, 2019 at 03:16 AM
You declared this static singleton variable:
public static Dialogues instance;
but you never assign any value to this variable. You should initialize it in Awake:
void Awake()
{
instance = this;
}
Oh thank you, I forgot to write that.. The error is gone after initializing the variable, but for some reason the text box is not showing any text. In line 36 of the 2nd script I added a print for completeText to see what text is being assigned and its printing the correct text from the list in Dialogues script.
I'm not sure why the Text box its not showing the message though, could it be because I'm using OnTriggerStay in the 2nd script and a coroutine in Dialogues script?
Edit: I just tried using a coroutine inside the 2nd script with timers inside a while loop, but the text box is still not showing anything. while(stayTime <=20f) { //execute the same code above }
Ok I think its working now. I forgot to start the StartCoroutine right after assigning the dialogue. The text is displaying, but when it starts its a little jittery for some reason. I'm not sure whats causing it, maybe its has to do with using OnTriggerStay2D and IEnumerator.
Thanks a lot for your help