Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tperry1x · Feb 07, 2015 at 09:11 PM · vector3string formattingvector3 operations

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toddisarockstar · Feb 08, 2015 at 01:55 AM

 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(")","");


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tperry1x · Feb 08, 2015 at 07:45 PM 0
Share

Thanks toddisarockstar. Now works a treat. $$anonymous$$y entire finished script looks like this if anyone is interested.

 // to make a long story short, assume the variable myplayerloc already exists, and contains data. For example
 var myplayerloc = "(-3.2, 12.0, 26.5)";
 
  
 // ignore parenthesis
 var restoreplayerpos : String = myplayerloc;
 var foundS1: int = restoreplayerpos.IndexOf("(");
 restoreplayerpos = restoreplayerpos.Substring(foundS1+1, restoreplayerpos.Length - 2 - foundS1);
     
 // now format position of player so we can restore the gameobject
 var sStrings = restoreplayerpos.Split(","[0]);
 var myposx : float = float.Parse(sStrings[0]);
 var myposy : float = float.Parse(sStrings[1]);
 var myposz : float = float.Parse(sStrings[2]);

 // find the player object and set it's location
 var playerObject = gameObject.Find("player");
 playerObject.transform.position = new Vector3(myposx, myposy, myposz);    
avatar image
0

Answer by AngryBurritoCoder · Feb 07, 2015 at 09:20 PM

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tperry1x · Feb 07, 2015 at 09:36 PM

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryBurritoCoder · Feb 07, 2015 at 10:02 PM 1
Share

it doesnt mean the same thing because you need to convert the strings into floats and put into vector3 if you get what i mean, because atm it is same as saying = Vector3(text,text,text);

avatar image tperry1x · Feb 07, 2015 at 10:08 PM 0
Share

Ok, that makes sense. So, silly question as I'm new at all this, but how do I simply convert the restoreplayerpos variable containing -7.4, 1.4, 6.5 to floats as needed?

avatar image tperry1x · Feb 07, 2015 at 11:03 PM 0
Share
 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?


avatar image
0

Answer by toddisarockstar · Feb 08, 2015 at 01:55 AM

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(")","");
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slerp to make the right/left side face another object 2 Answers

Is it posible to attach a crosshair to an image in vuforia? 0 Answers

assigning vector 3 through scene positioning 0 Answers

How can I assign an Array of Vector3 while seeing and editing the values in the Scene? 0 Answers

FPS Controller Confusion... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges