- Home /
How to make sprites instantiate based on randomly generated integer?
I have sprite numbers here:
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..
I got some of it working using arrays like you guys mentioned, but now the sprites instantiate on top of each other... like this:
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
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.)
$$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
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;
//...
}
}
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!
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!
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