Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by tperry1x · Feb 07, 2015 at 11:05 PM · vector3floats

Convert string variable into floats?

 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?


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

2 Replies

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

Answer by keburanuil · Feb 08, 2015 at 12:31 AM

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);


Comment
Add comment · Show 4 · 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 DaiMangouDev · Feb 08, 2015 at 12:48 AM 0
Share

your answer is much cleaner then $$anonymous$$e ^^ I totally forgot about Split.

avatar image tperry1x · Feb 08, 2015 at 08:37 AM 0
Share

Thank you both. I'll try this when I'm able to this evening and see how it goes. $$anonymous$$uch appreciated as always.

avatar image fafase · Feb 08, 2015 at 08:42 AM 0
Share

Your answer would get closer to perfect with a try/catch section.

avatar image tperry1x · Feb 08, 2015 at 06:32 PM 0
Share

Thank you keburanuil. That has worked brilliantly. Thanks to everyone else too, have chosen keburanuil's answer as it seemed the most efficient.

avatar image
0

Answer by DaiMangouDev · Feb 08, 2015 at 12:43 AM

          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[i] = float.Parse(TheString[i]);
                      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.

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 reckless_glitch · May 10, 2020 at 03:18 PM 0
Share

I know its old but: this version is very lazy and you are setting the values about 12 times, 9 times in your loop, what for? if you do this with more than 1 vector it will be a vaste of performance.

either that:

         Vector3 restoreplayerpos = new Vector3();
 
         for (int i = 0; i < TheString.Length; i++)
         {
             VectorPosition[i] = float.Parse(TheString[i]);
         }
 
         restoreplayerpos.x = VectorPosition[0];
         restoreplayerpos.y = VectorPosition[1];
         restoreplayerpos.z = VectorPosition[2];
 
         var playerObject = GameObject.Find("player");
 
         playerObject.transform.position = restoreplayerpos;

or that:

         for (int i = 0; i < TheString.Length; i++)
         {
             VectorPosition[i] = float.Parse(TheString[i]);
         }
 
         var playerObject = GameObject.Find("player");
 
         playerObject.transform.position = new Vector3(VectorPosition[0], VectorPosition[1], VectorPosition[2]);
 

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

22 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 avatar image

Related Questions

Checking if a Vector3 is a int 0 Answers

Missing Assembly References. 0 Answers

Why does this code not work? 1 Answer

Applying force without rigidbody VRTK tutorial question 0 Answers

I need help teleporting my object around. 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