- Home /
Object Instantiate
Hii..
I am having 3 types of object suppose red , green and blue.
I want to instantiate like red should come maximum amount of time where as green and blue object count will be less.
And once green or blue object instantiated then next one should not be repeated.
For that i have written logic like this :
void Update()
{
int count = Random.Range(0 , 5);
if(count == 0)
{
// blue
}
else if(count == 1)
{
// green
}
else
{
// red
}
}
But here i want to avoid repetition of green and blue object/.
This is rough logic i have written. anybody is having idea for this ?
Thanks.
Answer by Scribe · Aug 14, 2015 at 11:36 AM
just save what was last instantiated, and if you are about to do the same, pick again!
int lastPick = -1;
int count;
void Update(){
while(true){
count = Random.Range(0 , 5);
if(count == 0 && lastPick != 0){
// blue
lastPick = count;
break;
}
if(count == 1 && lastPicked != 1){
// green
lastPick = count;
break;
}
if(count > 1){
// red
lastPick = count;
break;
}
}
}
Scribe
this will only account for the two latest not being duplicates tho. you might need to add the latest added objects to a queue container.
She only asked for the first two not to be duplicated, not the reds... that's why i wrote it this way, if you want none duplicated just add another check for if the lastPick is > 1 on line 19 as well
Your answer
Follow this Question
Related Questions
Instantiate(GameObject) VS Instantiate(Resource.Load(Object Path)) 1 Answer
How do i Instantiate gameObjects in between multiple points? 2 Answers
How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers
[Solved] How to instantiate GameObject with different rigidbody parameters 1 Answer
Creating a GameObject variable without instantiating it? 1 Answer