- Home /
How to stop instantiating a certain object / objects from a list .
My code instantiates a random ball but I want to exclude a ball/balls in the run time on certain conditions ,How to do that ?
public Ball[] balls;
Ball randomBall = balls[Random.Range(0, balls.Length)];
public void InstantiateBall()
{
Ball randomBall = balls[Random.Range(0, balls.Length)];
ballCountInScene = FindObjectsOfType<Ball>().Length;
if (ballCountInScene == 0 )
{
ballDestroyed = true;
Instantiate(randomBall , spawnPoint.position, Quaternion.identity);
}
else
{
ballDestroyed = false;
}
}
When do you call InstantiateBall()? Also, is it intentional that you declare "Ball randomBall" twice? How do you populate balls[]?, in the inspector?
Seems this method will create another ball duplicate from the array once if there are no balls in the scene, and that includes the balls in balls[] also I think?
I think you have to think this through, what is it that you want to do?
Answer by Hellium · Feb 08, 2020 at 06:43 PM
Following code not tested
private List<Func<GameObject,bool>> conditions = new List<Func<GameObject,bool>>();
public Ball[] balls;
public void InstantiateBall()
{
List<GameObject> spawnableBalls = GetSpawnableBalls();
if(spawnableBalls.Count == 0)
return ;
Ball randomBall = spawnableBalls[Random.Range(0, spawnableBalls.Count)];
ballCountInScene = FindObjectsOfType<Ball>().Length;
if (ballCountInScene == 0 )
{
ballDestroyed = true;
Instantiate(randomBall , spawnPoint.position, Quaternion.identity);
}
else
{
ballDestroyed = false;
}
}
private List<GameObject> GetSpawnableBalls()
{
List<GameObject> spawnableBalls = new List<GameObject>();
for( int i = 0 ; i < balls.Length ; ++i)
{
if(IsSpawnable(balls[i]))
spawnableBalls.Add( balls[i] );
}
return spawnableBalls;
}
private bool IsSpawnable(GameObject ball)
{
for( int i = 0 ; i < conditions.Count ; ++i)
{
if(conditions[i](ball))
return false;
}
return true;
}
// Example to stop spawnling blue balls
public void StopSpawningBlueBalls()
{
conditions.Add(new Func<GameObject,bool>( ball => ball.GetComponent<Renderer>().color == Color.blue ));
}
// Example to stop spawnling big balls
public void StopSpawningBigBalls()
{
conditions.Add(new Func<GameObject,bool>( ball => ball.transform.lossyScale.sqrMagnitude > 100 ));
}
// Implement the other functions you need in order to exclude the balls under certain conditions
Answer by tormentoarmagedoom · Feb 08, 2020 at 12:51 PM
hey there.
you can just add a if line before instantiate to decide if stop the function.
if(something) return;
if return is executed, InstantiateBall() function will be stoped fpr that iteration and code "jumps" to next part of the code.
Is this what you wanted?
Byee
I don't want to stop the function , I want to change the variables in the list , like let's say I have a blue , red and a yellow ball and I want to stop instantiating the blue ones in a certain condition . thanks for replying .
Answer by lvjuvan · Feb 08, 2020 at 05:00 PM
Have subclasses for each type of ball, e.i. classes inheriting from the Ball class and then use the typeof operator to determine if you want to instantiate.,Not sure if I understand the question, but one way of doing it would be to have subclassess of ball, e.g. blue ball, red ball and so on, all inheriting from ball and use the typeof operator to check if it should be instantiated or if it should try again.
Or, have several arrays with the different types of balls, and compile the ones that you want to allow to a list and then take a random entry from that list.