Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RoboticSarcasm · Jul 09, 2015 at 11:30 AM · c#stringmath

Issue with seed-based generation [C#]

Hello everybody,

after tinkering around with seed-based generation, i have run into an issue. I use part of a float and turn it into a string, but the string always becomes something along the lines of 2.090868e+18 which for me is useless, because i use a float to generate boxes in a specific location. After a couple of boxes being spawned, i obviously run into the problem of reaching a letter instead of a number, and i have not a single clue as of how i can fix this. I hope you guys do. Code:

 using UnityEngine;
 using System.Collections;
 using System.Text;
 using System.IO;
 
 public class gameSystems : MonoBehaviour {
     
     public Transform boxToSpawn;
 
     public float timeLeft;
     
 
     public int score;
     public AudioClip pickupAudio;
 
 
     /////////BOX SPAWNING VARIABLES/////////////
     public float generatedSeed;
     public string seedTempString;
     public char seedChar;
     public string seedCharString;
     public int boxesGenerated;
     public float boxSpawnPos;
     public float boxPreviousSpawnPos;
 
 
     void Awake()
     {
         GameObject.Find("gameSystems").SendMessage("SaveGame", SendMessageOptions.DontRequireReceiver);
         DontDestroyOnLoad(transform.gameObject);
         boxesGenerated = 1;
         GenerateSeed();
         CalculateSpawnPos();
         Time.timeScale = 1;
          }
 
 
 
     
 
     
     
 
     
 
     void AddScore(int scoreAdded)
     {
         audio.PlayOneShot(pickupAudio);
         score += scoreAdded;
         CalculateSpawnPos();
         {
             GameObject.Find("gameSystems").SendMessage("SaveGame", SendMessageOptions.DontRequireReceiver);
         }
         
 
 
 
 
 
     }
 
 
     void GenerateSeed()
     {
 
         generatedSeed = Random.Range(1000000000000000000, 10000000000000000000);
 
     }
 
     void CalculateSpawnPos()
     {
 
        
         if (boxSpawnPos == 0) 
         {
             boxSpawnPos = 1;
             
         }
         if (boxSpawnPos == boxPreviousSpawnPos) 
         { 
             boxSpawnPos += 1;
             
         }
         spawnBox(); 
     }
 
 
 
     void FixedUpdate()
     {
         seedTempString = generatedSeed.ToString();
         seedChar = seedTempString[boxesGenerated];
         seedCharString = seedChar.ToString();
         boxSpawnPos = float.Parse(seedCharString);
         timeLeft -= Time.deltaTime;
         if (timeLeft <= 0) { Application.LoadLevel(Application.loadedLevel); }
     }
 
     
 
     void spawnBox()
     {
         
         
             boxesGenerated += 1;
             if (boxSpawnPos == 1) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn1").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 2) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn2").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 3) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn3").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 4) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn4").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 5) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn5").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 6) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn6").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 7) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn7").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 8) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn8").transform.position, Quaternion.identity); }
             if (boxSpawnPos == 9) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn9").transform.position, Quaternion.identity); }
             boxPreviousSpawnPos = boxSpawnPos;
         }
                 
         
     }
 
 

Thank you for taking the time to help me, and i hope i can at some point help you.

Greetings,

Roel Zwakman / RoboticSarcasm

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

3 Replies

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

Answer by Nischo · Jul 09, 2015 at 11:59 AM

The e is not useless, its a way to display a float, read about it here. Are more mathematically correct explanation can be found here

Now to fix your problem, the simple solution would be to use

 seedChar.ToString("F");

But this will turn your number into a big string with trailing/leading zeros. Which is correct, because you want to generate numbers way bigger than possible. Your max number for example is 10.000.000.000.000.000.000 which reads 10 quintillion. Way more than a computer can conventionally store or work with. Ask yourself why you would need such a big number and why it must be float.

Edit: "F" is correct, not "#".

Comment
Add comment · Show 2 · 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 malkere · Jul 09, 2015 at 12:02 PM 0
Share

in quotes? man I never would have stumbled across that! cool to know though. I think he doesn't know he has such a tiny number.

avatar image RoboticSarcasm · Jul 09, 2015 at 12:13 PM 0
Share

this only gives me more errors actually, haha. I am currently trying to wrap my head around Lists because a friend recommended trying to use those, which sounded like a good idea. Still though, thank you very much

avatar image
1

Answer by malkere · Jul 09, 2015 at 11:55 AM

you mean e-18?

Long Answer:

what that means is there are 18 0's before the 2. knowing that you could write a calculation to see if

 if (yourString.SubString(yourString.length - 2, 1) == "-") {
    //this means you have a *****e-** string
 }
 else if (yourString.SubString(yourString.length - 1, 1) == "-") {
    //this means you have a *****e-* string
 }

and if either of those are true make a string with whatever the yourString ends in, for example 18, zeros you could that with

 for (int i = 0; i < zeroCount; i++) {
    newString += "0";
 }

and then append the 2, skip the period, and add the rest.

Short Answer:

with a number that small though, I think it's trying to tell you that you're at zero =D

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
1

Answer by ByteSheep · Jul 09, 2015 at 12:11 PM

http://stackoverflow.com/questions/3757945/how-to-get-floats-value-without-including-exponential-notation

You can pass a parameter to the ToString method:

 seedTempString = generatedSeed.ToString("F");

As a side note you would probably want to have an array of spawn points rather than using the Find() method for performance reasons. Also you could simplify the spawnBox code to:

 void spawnBox()
 {
    boxesGenerated++;
    Instantiate(boxToSpawn, GameObject.Find("boxSpawn" + boxSpawnPos).transform.position, Quaternion.identity);
    boxPreviousSpawnPos = boxSpawnPos;
 }

It seems like you'd want to move the boxSpawnPos calculations from the FixedUpdate function to the CalculateSpawnPos method, since this doesn't need to be calculated every frame. Anyway, I hope this solves your question about storing a float value in a string :)

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 RoboticSarcasm · Jul 09, 2015 at 12:15 PM 0
Share

little comment on the side note: the reason it's in FixedUpdate is because doing it any other way managed to throw me errors haha

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

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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Making my animation work (error hash does not exist) 0 Answers

Assigning String Value From Array in separate script sets value to null. 2 Answers

Array not displaying my level best time in my level select menu 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