I’m setting up a dialog system for my game, and I’d like to make it so that when there’s a string with “!player” in it, it replaces that with a name the player has entered. The way I have it set up is with scriptable objects feeding into a UI canvas. Currently my Scriptable Object looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(fileName = "New Dialog", menuName = "Dialog Boxes")]
public class Dialog : ScriptableObject
{
public string playerName;
public string playerPronouns;
public string talkeeName;
public string talkeePronouns;
public string[] dialogEntries;
public Sprite[] playerPortraits;
public Sprite[] talkeePortraits;
private void Awake()
{
dialogEntries[0] = dialogEntries[0].Replace("!player", "you");
}
}
And my UI canvas script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogDisplay : MonoBehaviour
{
public Dialog dialog;
public TextMeshProUGUI playerName;
public TextMeshProUGUI talkeeName;
public TextMeshProUGUI playerPronouns;
public TextMeshProUGUI talkeePronouns;
public Image playerImage;
public Image talkeeImage;
public TextMeshProUGUI dialogText;
public int dialogLength;
// Start is called before the first frame update
void Start()
{
playerName.text = dialog.playerName;
talkeeName.text = dialog.talkeeName;
playerPronouns.text = dialog.playerPronouns;
talkeePronouns.text = dialog.talkeePronouns;
playerImage.sprite = dialog.playerPortraits[0];
talkeeImage.sprite = dialog.talkeePortraits[0];
dialogText.text = dialog.dialogEntries[0];
dialogLength = -1;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
dialogLength++;
{
if(dialogLength < dialog.dialogEntries.Length)
{
dialogText.text = dialog.dialogEntries[dialogLength];
}
else if(dialogLength >= dialog.dialogEntries.Length)
{
dialogLength = -1;
gameObject.SetActive(false);
}
}
}
}
}
Right now I’m just trying to have it where it replaces “!player” with “you” in the scriptable object script on the first string, but it won’t even do that. It’s not returning any errors but it’s also simply not doing anything to the string. Does anyone have a good method for doing this?