Restore player position using vector3 from variable.

Hello there. Hoping someone can help me.

I’m trying to restore the player position from a variable.

I’m trying to do it in JS as that’s what I’ve got a hope of understanding.

The code I have so far is as follows:

// assume the variable 'restoreplayerpos' already contains the string "-17.6, 1.4, 21.2" without the quotes.

// restore player position
     var playerObject = GameObject.Find("player");
     playerObject.transform.position = Vector3(restoreplayerpos);

When I run the script I get an error:

"The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(String)'."

I need to convert the variable to what is expected as a Vector3 somehow, but don’t know how.
I’ve been googling this for ages. Please can someone help.

You cannot put a string into a Vector3, basically instead of assigning a string assign a Vector3 variable doing it like this ( same as making a int or string no difference )

public Vector3 playerResetPos = new Vector3(-17.6f, 1.4f, 21.2f); ( cant remember if there is new or not in front, my memory dissapears sometimes)
now you can use this…

playerObject.transform.position = playerResetPos;

Wow, that was quick. Thanks!

Did what was suggested, but now I’m getting the three following errors in unity:

expecting }, found 'public'.
';' expected. Insert a semicolon at the end.
expecting EOF, found '}'

So, just to explain. My script works like this. It’ll read a file in the subfolder ‘data’ and looks for ‘mydata.txt’.
It’ll then read the second line of that data, which currently contains:

(-7.4, 1.4, 6.5)

It’ll then remove the brackets.
Then it stores -7.4, 1.4, 6.5 in the restoreplayerpos variable
Everything fails when I try to restore the player position based on this variable.
I’ll get this error:

'The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(String)'.

I assume this is because Unity isn’t formatting the variable somehow?

Strangely enough, I can type

playerObject.transform.position = Vector3(-7.4, 1.4, 6.5);

and that’ll work absolutely fine, even though the variable means the same thing!

that last answer i believe was in C#
which is giving you additional errors when its in your javascript.

the whole problem is that a vector3 variable contains a set of three seperate NUMBERS.
restoreplayerpos is a string variable. so the the computer sees it as just one variable of text characters that dont nessisarily have a mathmatical value. So your problem is more complex than you think! I can try to get you in the right direction. it is possible to convert your string to the three numeric values to form a Vector3.

if your restoreplayerpos is formated exactly like this -17.6, 1.4, 21.2
this code will split your string at the commas and push the three variables into an array called words.

     var words = new Array();
     words = restoreplayerpos.Split(","[0]);

now you have 3 seperate strings for your vector3 and eliminated the commas characters so we can convert them out of string format.

var posx:float; 
var posy:float; 
var posz:float; 

 float.TryParse(words[0],posx);
  float.TryParse(words[1],posy);
   float.TryParse(words[2],posz);

now we got 3 actual numbers with a value out of the string so lets put them together to make a real Vector3.

var realvector:Vector3;
realvector=Vector3(posx,posy,posz);

playerObject.transform.position = realvector;

just paste these lines in your code to declair your position. it will work. read up on strings!

var posx:float; 
  var posy:float; 
  var posz:float; 
 var words = new Array();
  var realvector:Vector3;
 
 words = stringyouarecoverting.Split(","[0]);
 
  
   float.TryParse(words[0],posx);
   float.TryParse(words[1],posy);
   float.TryParse(words[2],posz);
 
 
  realvector=Vector3(posx,posy,posz);
  
  
  playerObject.transform.position = realvector;

and btw,if your string contains anything else like brackets this wont work you would have to use an extra line like this before you convert to eliminate whatever other characters in the string.

 nameofstingtoconvert.Replace("(","");
 nameofstingtoconvert.Replace(")","");

var posx:float;
var posy:float;
var posz:float;
var words = new Array();
var realvector:Vector3;

words = stringyouarecoverting.Split(","[0]);

 
  float.TryParse(words[0],posx);
  float.TryParse(words[1],posy);
  float.TryParse(words[2],posz);


 realvector=Vector3(posx,posy,posz);
 
 
 playerObject.transform.position = realvector;

if your string contains anything else like brackets this wont work you would have to use an extra line like this before you convert to eliminate whatever other characters in the string.

nameofstingtoconvert.Replace("(","");
nameofstingtoconvert.Replace(")","");