I am making a simple text adventure game to learn unity. I have the following code which creates a typewriter effect on text.
public class Dialog : MonoBehaviour
{
public TextMeshProUGUI textdisplay;
public string senetnces;
private int index;
public float typingspeed;
private AudioSource textScrollingSound;
private void Start()
{
StartCoroutine(Type());
textScrollingSound = transform.Find("textScrollSound").GetComponent<AudioSource>();
textScrollingSound.Play();
}
IEnumerator Type()
{
foreach (char letter in senetnces.ToCharArray())
{
textdisplay.text += letter;
if (letter.Equals('.') || letter.Equals(',') || letter.Equals('!') || letter.Equals('?'))
{
if (Input.GetKey("space") == false)
{
textScrollingSound.Stop();
yield return new WaitForSeconds(.25f);
textScrollingSound.Play();
}
}
yield return new WaitForSeconds(typingspeed);
}
}
private void Update()
{
if (Input.GetKeyDown("space"))
{
typingspeed = .002f;
textScrollingSound.pitch = 2;
}
if (Input.GetKeyUp("space"))
{
typingspeed = .02f;
textScrollingSound.pitch = 1;
}
}
}
I am now following the Unity made text adventure tutorial (
).
The tutorial says to make a ScriptableObject class called room, to create different rooms for the player to enter.
[CreateAssetMenu(menuName = "TextAdventure/Room")]
public class Room : ScriptableObject
{
[TextArea]
public string description;
public string roomName;
}
I want to get the string “description” into my class Dialog so I can use the typewriter effect on the description of each room. I am not sure how to access the “description” variable from another class since the variable is in a ScriptableObject class.
Any help would be greatly appreciated!