# More efficient

**URL:** https://discussions.unity.com/t/more-efficient/421649
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 27, 2010, 10:56pm UTC](https://discussions.unity.com/t/more-efficient/421649 "2010-08-27T22:56:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mattcscz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mattcscz/32/435025_2.png) [@mattcscz](https://discussions.unity.com/u/mattcscz)
#### Post date: [August 27, 2010, 10:56pm UTC](https://discussions.unity.com/t/more-efficient/421649/1 "2010-08-27T22:56:46Z")

</div>

hey guys, i just quickly made a script to allow me to change my level through a text box, however i feel instead of using say “.ModLevel 2” i should be able to enter any amount.  
for example if i entered “.ModLevel 10” i want to be able to add 10 without having “.ModLevel 10” as a pre defined variable.

heres the script :

```auto
var PlayerLevel =1;
var TextEnter = "Enter Text";
var Mod1 = ".ModLevel 1";
var Mod2 = ".ModLevel 2";

var B1 = 10;
var B2 = 20;
var B3 = 60;
var B4 = 50;
var E1 = 20;
var E2 = 20;
var T1 = 20;
var T2 = 20;
var T3 = 20;
var T4 = 20;

var ShowTextBox : boolean = true;

function Update () {
if(TextEnter == Mod1 Input.GetKeyDown("return")) {
	ShowTextBox = false;
	PlayerLevel = 1;
	TextEnter = "Enter Text";
	TextBox ();
	}
if(TextEnter == Mod2 Input.GetKeyDown("return")) {
	ShowTextBox = false;
	PlayerLevel = 2;
	TextEnter = "Enter Text";
	TextBox ();
	}
}

function OnGUI () {
	GUI.Label (Rect(B1,B2,B3,B4),""+ (Money.Gold ));
	GUI.Label (Rect(E1,E2,B3,B4),"Lv." + (PlayerLevel));
	if(ShowTextBox) {
	TextEnter = GUI.TextField (Rect (T1,T2,T3,T4), TextEnter, 50);
	}
}

function TextBox () {
ShowTextBox = true;
}

```

---

<div class="post-metadata">

### Author: ![mattcscz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mattcscz/32/435025_2.png) [@mattcscz](https://discussions.unity.com/u/mattcscz)
#### Post date: [August 28, 2010, 12:05am UTC](https://discussions.unity.com/t/more-efficient/421649/2 "2010-08-28T00:05:23Z")

</div>

hers the code in pesudo:

if(i enter .modlevel “amount”) {  
level = amount;  
}

---

<div class="post-metadata">

### Author: ![mattcscz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mattcscz/32/435025_2.png) [@mattcscz](https://discussions.unity.com/u/mattcscz)
#### Post date: [August 30, 2010, 1:59am UTC](https://discussions.unity.com/t/more-efficient/421649/3 "2010-08-30T01:59:19Z")

</div>

if it helps, i knew you could do somthing like this in python, i can’t rember exactly but you can build strings using parts like {0} walked to a town and {1} {2}

0,1 and 2 would be replaced with somthing, but you could also insert stuff with a similar option and it would be stored as a variable

---

<div class="post-metadata">

### Author: ![andeeeee](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@andeeeee](https://discussions.unity.com/u/andeeeee)
#### Post date: [August 31, 2010, 3:34pm UTC](https://discussions.unity.com/t/more-efficient/421649/4 "2010-08-31T15:34:44Z")

</div>

There are a couple of functions in the .NET library that will help here.  
[String.LastIndexOf](http://msdn.microsoft.com/en-us/library/1wdsy8fy.aspx) returns the rightmost position in the string where a particular character or substring occurs. You can use this to detect where the space is in your source string. Then, you can use [String.Substring](http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx) to extract the portion that follows the space. Finally, [Int32.Parse](http://msdn.microsoft.com/en-us/library/system.int32_members.aspx) will convert the numeric string into an integer, assuming it is correctly formed:-

```auto
var pos = TextEnter.LastIndexOf(" ");
var numString = TextEnter.Substring(pos + 1);
var num = Int32.Parse(numString);

```
