- Home /
Can you use a variable in the text editor of the unity inspector?
I have no idea how to google for it, so I try my luck here:
I have an array of strings and in the unity inspector I wite down lines of text for the different items of the array. Can I refer to another string inside of the inspector like:
Item 3: This is an example with my name, which is {playername} , to show what I mean
In c# it would be somthing like
new string[] textline;
new string playername = "Marcel";
textline[2] = "This is an example with my name, which is " + playername + " to show what I mean";
Answer by Hellium · Jun 01, 2019 at 04:23 PM
Sure it is possible.
Two solutions, either you use the String.Format, and you put {0}
everywhere you need to insert the name : "This is an example with my name, which is {0}"
private void Start()
{
for( int i = 0 ; i < textline.Length ; ++i )
textline[i] = string.Format( textline[i], playername ) ;
}
Or you use a special string to replace using String.Replace : "This is an example with my name, which is {playerName}"
private void Start()
{
for( int i = 0 ; i < textline.Length ; ++i )
textline[i] = textline[i].Replace("{playerName}", playername ) ;
}
Your answer
Follow this Question
Related Questions
Select TextField when it gets focus (in custom editor) 0 Answers
PlayerPrefs clashing with TextField? 2 Answers
GUI textfield duplicate text 0 Answers
How to check if control is pressed in a text field? 2 Answers
Change Colour from String 2 Answers