I have a strange problem this being that when i use String.Split it wont display the other text which is held in an array?
This is my code:
var Text : TextAsset;
var arr = new Array();
arr = Text.text.Split("..."[0]);
print (arr[0]);
And this is what my text(txt) file holds:
But if i use this code it will print “Hello, My name is NPC 1” but is i change print (arr[0]) to print (arr[1]) it doesn’t work? when i would have though it would print “Hello I’m NPC 2” could someone please give some incite onto what is happening?
When you write “…”[0] on line 3, you’re really just passing a ‘.’ character as the delimiter. So it will split your string at every ‘.’, not look for three in a row. There is an option to ignore empty strings, but it’s not really what you’re looking for.
What you really want to do is pass an array of delimiter strings, so that Split will only break the input string when it finds the whole of a delimiter string. Most of the Split variants only look for single characters.
Something like this though:
var delimiters = new String[1];
delimiters[0] = "...";
arr = Text.text.Split(delimiters, System.StringSplitOptions.None);
This will split your string only where three dots appear in a row.
Perhaps UnityScript has a more concise way to construct an array of strings with a single element… but I don’t know what it is.
Really? it is working? i just get a bunch of chars
(I.E. “I”. “:”, “:”, “:”, “L”, etc…)…
var stringtext = ("I:::Lost:::My:::Cat");;
var delimiters = new String[1];
delimiters[0] = ":::";
stringtext.Split(delimiters, System.StringSplitOptions.None);
kilt, in your code snippet you didn’t store the result of the Split call. Where did you get the chars from?
Here’s working code, based on yours:
#pragma strict
function Start()
{
var stringtext = "I:::Lost:::My:::Cat";
var delimiters = new String[1];
delimiters[0] = ":::";
var result = stringtext.Split(delimiters, System.StringSplitOptions.None);
for (var fragment in result)
{
print(fragment);
}
}
var Text : TextAsset;
private var TextArray = new Array ();
var NPCnum = 0;
private var delimiters = new String[1];
function Start()
{
delimiters[0] = "...";
TextArray = Text.text.Split(delimiters, System.StringSplitOptions.None);
}
//function OnGUI()
//{
// GUI.Box(Rect(Screen.width / 18,Screen.height / 2 - Screen.height / 18,Screen.width * 0.9,Screen.height / 2),TextArray[NPCnum]);
//}
function OnTriggerEnter (other : Collider)
{
var NPCStats = other.gameObject.GetComponent(NPC);//(may be broken)
NPCnum = NPCStats.NPCnum;
GUI.Box(Rect(Screen.width / 18,Screen.height / 2 - Screen.height / 18,Screen.width * 0.9,Screen.height / 2),TextArray[NPCnum]);
}
And this was for testing purposes(does work)
var arr = new Array();
var NPCnumb = 0;
var delimiters = new String[1];
delimiters[0] = "...";
arr = Text.text.Split(delimiters, System.StringSplitOptions.None);
print (arr[NPCnumb]);