- Home /
One of the numbers as Second ?
Hello again, I want to ask a question about random methods. I want to create an object at random seconds.
Such like ;
1st object instantiates within 3 seconds 2nd object instantiates seconds between 3 & 6 seconds
how can i do that using random method ? sorry for my bad english.
Answer by Jamora · Aug 03, 2013 at 05:37 PM
Yes, you can use either the System.Random class or Unity's own Random class.
To get a random number between any two numbers, say x and y ( x
So to make this concrete:
your first object would get a random number as Random(0f,3f);
your second would be 3f + Random( 0f,(6-3)f );
The "f"s mean floats, in case you only want integers you should leave them out.
I've changed my idea about code. I want to apply this like this code below:
var randomNumber : int;
function Update()
{
ChosenOne();
}
function ChosenOne()
{
randomNumber = Random.Range(1, 4);
Debug.Log(randomNumber.ToString());
yield WaitForSeconds(3 + randomNumber);
}
how can i apply this as you said ? Could you edit ? thanks a lot :)
$$anonymous$$y answer explains exactly how to achieve a random number from what ever continuous range you choose. I will try to explain further, but not write your code for you.
You must first know how much of your value is random and how much will always happen. If you want to get a value between 3 and 6; 1, 2 and 3 can be considered as always happening, and the random element is either 0,1,2 or 3. Because the first 3 always happen, you only need to add the random element (0 to 3) to it.
You need to read my answer(s) again and then apply the explained basic principle to your problem.
Your answer
Follow this Question
Related Questions
Random array issue C# 2 Answers
Instantiate a random prefab at an objects location 3 Answers
Calling 2 specific random float numbers in random.range using array not working! 2 Answers
Randomly instantiate objects from array without choosing the same item twice. 2 Answers
Instantiate Prefab at random 1 Answer