samedi 15 décembre 2018

Replacing dynamic variable in string UNITY

I am making a simple dialogue system, and would like to "dynamise" some of the sentences. For exemple, I have a Sentence

Hey Adventurer ! Welcome in the world !

Now In code I am trying to replace that by the real value of the string in my game. I am doing something like this. But it doesn't work. I do have a string PlayerName in my component where the function is situated

Regex regex = new Regex("(?<=)");
MatchCollection matches = regex.Matches(sentence);

for(int i = 0; i < matches.Count; i++)
{
    Debug.Log(matches[i]);
    sentence.Replace("+matches[i]+", this.GetType().GetField(matches[i].ToString()).GetValue(this) as string);
}
return sentence;

But this return me an error, even tho the match is correct.

Any idea of a way to do fix, or do it better?





Aucun commentaire:

Enregistrer un commentaire