Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yaezah · Sep 19, 2017 at 07:17 PM · instantiatetransformprefabcomponentrandom.range

How to make sprites instantiate based on randomly generated integer?

I have sprite numbers here:

alt text

I would like to know if it is possible to generate them based on a randomly generated integer.

For example, if "Random.Range (1, 1000)" generates the number 250, then the 2 , the 5, and the 0 from the spritesheets are instantiated in that order..

Comment
Add comment · Show 5
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 yaezah · Sep 20, 2017 at 12:14 AM 0
Share

I got some of it working using arrays like you guys mentioned, but now the sprites instantiate on top of each other... like this:

alt text

This is the script I have so far:

void numbers() {

     string str = _gold.ToString();

     for (int i = 0; i < str.Length; i++)
     {
         switch (str[i])
         {
             case '0':
                 Instantiate(Resources.Load("0"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '1':
                 Instantiate(Resources.Load("1"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '2':
                 Instantiate(Resources.Load("2"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '3':
                 Instantiate(Resources.Load("3"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '4':
                 Instantiate(Resources.Load("4"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '5':
                 Instantiate(Resources.Load("5"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '6':
                 Instantiate(Resources.Load("6"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '7':
                 Instantiate(Resources.Load("7"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '8':
                 Instantiate(Resources.Load("8"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
             case '9':
                 Instantiate(Resources.Load("9"), transform.position + Vector3.up * 0.5f, Quaternion.identity);
                 break;
         }


$$anonymous$$ost of my gold is 1 to 3 digits, so is there a way to make it get the last instantiated number's transform and position the next number an x amount of position to the right

avatar image Arocide yaezah · Sep 20, 2017 at 12:42 AM 0
Share

The switch statements are unnecessary, you can just use a while statement and some Arithmetic like this for example:

         int digit = Random.Range(1, 1000);
         do
         {
             Instantiate(Resources.Load((digit % 10).ToString()), transform.position + Vector3.up * 0.5f, Quaternion.identity);
             digit /= 10;
         } while (digit > 0);

would render your sprites in the same way as what you've written there. (Note: using this method it will render the right most digit first and go backwards. e.g. 250 would instantiate as 0, 5, 2 so be $$anonymous$$dful of your offsets to do it the right way.)

avatar image yaezah Arocide · Sep 20, 2017 at 01:04 AM 0
Share

$$anonymous$$an I wish I would've read your reply 10 $$anonymous$$utes ago... this is what I did in that 10 $$anonymous$$utes lol...

Gonna try yours it seems way simpler

         string str = _gold.ToString();
 
         for (int i = 0; i < str.Length; i++)
         {
             switch (str[i])
             {
                 case '0':
                     if (firstnumber == true)
                     {
                         Instantiate(zero, transform.position, Quaternion.identity);
                         firstnumber = false;
                         secondnumber = true;
                     }
                     else if (secondnumber == true)
                     {
                         Instantiate(zero, transform.position + Vector3.right * 0.3f, Quaternion.identity);
                         thirdnumber = true;
                         secondnumber = false;
                     }
                      else if (thirdnumber == true)
                     {
                         Instantiate(zero, transform.position + Vector3.right * 0.6f, Quaternion.identity);
                         fourthnumber = true;
                         thirdnumber = false;
                     }
                      else if (fourthnumber == true)
                     {
                         Instantiate(zero, transform.position + Vector3.right * 0.9f, Quaternion.identity);
                         fourthnumber = false;
                     }
 
                     break;
 
                 case '1':
                     if (firstnumber == true)
                     {
                         Instantiate(one, transform.position, Quaternion.identity);
                         firstnumber = false;
                         secondnumber = true;
                     }
                     else if (secondnumber == true)
                     {
                         Instantiate(one, transform.position + Vector3.right * 0.3f, Quaternion.identity);
                         thirdnumber = true;
                         secondnumber = false;
                     }
                     else if (thirdnumber == true)
                     {
                         Instantiate(one, transform.position + Vector3.right * 0.6f, Quaternion.identity);
                         fourthnumber = true;
                         thirdnumber = false;
                     }
                     else if (fourthnumber == true)
                     {
                         Instantiate(one, transform.position + Vector3.right * 0.9f, Quaternion.identity);
                         fourthnumber = false;
                     }
 

Eight more times lol

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by · Sep 19, 2017 at 08:34 PM

Yes, it is possible. There must be a way to do this by getting every number, but I can't remember heheheh. This is a solution.

Put the randomNumber in a string

 String str = randomNumber.ToString()

then go to each char in that string and instantiate the correct number

 for (int i = 0; i < str.Length; i++)
 {
      switch (str[i])
      {
           case '1':    
           //Instantiate    
            break;    
           //...    
      }    
 }




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 IndievdB · Sep 19, 2017 at 08:39 PM 1
Share

You can have an array of sprites, where array[0] corresponds to the '0' sprite, etc. You can loop through every character of randomNumber.ToString(), use (int) Char.GetNumericValue() to convert that character to an int. Then use the int as the index in the Sprite array!

avatar image yaezah · Sep 19, 2017 at 10:08 PM 0
Share

Thanks for the code, I'm still a noob so I'll start researching how arrays works :) , but I was just wondering, do I have to separate the spritesheet into 10 different sprites , one for each number, or do I keep the spritesheet as one whole image? Thanks!

avatar image IndievdB yaezah · Sep 20, 2017 at 01:29 AM 1
Share

I think it would be easiest to import the whole image into Unity and in the import settings, split the image into 10 different sprites of equal size.

https://unity3d.com/learn/tutorials/topics/2d-game-creation/sprite-editor

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

114 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 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 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 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 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

The variable othertransform of Prefab has not been assigned 2 Answers

Instantiate issues 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Not your typical "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption" 0 Answers

problem whit Instantiate Prefabs position. 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