# Convert string variable into floats?

**URL:** https://discussions.unity.com/t/convert-string-variable-into-floats/130158
**Category:** Questions & Answers
**Created:** [February 7, 2015, 11:05pm UTC](https://discussions.unity.com/t/convert-string-variable-into-floats/130158 "2015-02-07T23:05:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tperry1x](https://avatars.discourse-cdn.com/v4/letter/t/a698b9/32.png) [@tperry1x](https://discussions.unity.com/u/tperry1x)
#### Post date: [February 7, 2015, 11:05pm UTC](https://discussions.unity.com/t/convert-string-variable-into-floats/130158/1 "2015-02-07T23:05:00Z")

</div>

print(restoreplayerpos);  
// variable contains -7.4, 1.4, 6.5

```
// restore player position
var playerObject = GameObject.Find("player");
playerObject.transform.position = Vector3(restoreplayerpos); // need to convert the variable restoreplayerpos into a float somehow?

```

---

<div class="post-metadata">

### Author: ![keburanuil](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/keburanuil/32/211121_2.png) [@keburanuil](https://discussions.unity.com/u/keburanuil)
#### Post date: [February 8, 2015, 12:31am UTC](https://discussions.unity.com/t/convert-string-variable-into-floats/130158/2 "2015-02-08T00:31:19Z")

</div>

You can use string.Split() for splitting the string values and float.Parse() for string to float conversion.

float.Parse() works even if the input string contains whitespaces. So " 1.4" will be converted without any errors.

Try this:

**Javascript**

```
var sStrings = restoreplayerpos.Split(","[0]);

var x : float = float.Parse(sStrings[0]);
var y : float = float.Parse(sStrings[1]);
var z : float = float.Parse(sStrings[2]);

var playerObject = gameObject.Find("player");
playerObject.transform.position = new Vector3(x, y, z);

```

**C#**

```
var sStrings = restoreplayerpos.Split(","[0]);

float x = float.Parse(sStrings[0]);
float y = float.Parse(sStrings[1]);
float z = float.Parse(sStrings[2]);

var playerObject = GameObject.Find("player");
playerObject.transform.position = new Vector3(x, y, z);

```

---

<div class="post-metadata">

### Author: ![DaiMangouDev](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/daimangoudev/32/339271_2.png) [@DaiMangouDev](https://discussions.unity.com/u/DaiMangouDev)
#### Post date: [February 8, 2015, 12:43am UTC](https://discussions.unity.com/t/convert-string-variable-into-floats/130158/3 "2015-02-08T00:43:08Z")

</div>

string TheString = { “-7.4”, “1.4”, “6.5” };  
float VectorPosition = new float[TheString.Length];  
Vector3 restoreplayerpos = new Vector3();

```
                  for (int i = 0; i < TheString.Length;i++ )
                  {
        
                     VectorPosition _= float.Parse(TheString*);*_

```

_restoreplayerpos.x = VectorPosition[0];_  
_restoreplayerpos.y = VectorPosition[1];_  
_restoreplayerpos.z = VectorPosition[2];_  
_}_

_var playerObject = GameObject.Find(“player”);_

_playerObject.transform.position = new Vector3( restoreplayerpos.x, restoreplayerpos.y,restoreplayerpos.z);_  
_why do you need it to be a string ??_  
_I didn’t test it but It should work._
