Insatiate a random object in a string only if that game object does not exist

hey, so I have 6 types of power-up drops that I would like to drop randomly. 2 of which are for different types of extra balls. I don’t want those to powerups to drop if either of them is active or if the balls themselves are active. how I have it set up now works with just the one ball but it also means that it will also always drop first and I don’t know how to add the second one without having a similar problem.

Bricks bs;

    if(bs = collision.gameObject.GetComponent<Bricks>())
    { 
            
            if (GameObject.FindGameObjectWithTag("Ball") != null || GameObject.FindGameObjectWithTag ("secondBall") !=null)
            {
                Instantiate(powerups[Random.Range(0, powerups.Length)], collision.transform.position, Quaternion.identity);
            }
        if (GameObject.FindGameObjectWithTag("Ball") == null && GameObject.FindGameObjectWithTag("secondBall") == null)
        {
            Instantiate(powerup);
        }


    }

I probably would need more context to give you a right answer and far more code than what you are showing here. But first, do this:

GameObject ball;
private void Awake() { ball = FindGameObjectWithTag("Ball");  }

If you are calling FindGameObjectWithTag this much you should probably start storing those things in a variable.

A solution to your problem: if you are trying not spawn things when the other one is active you should use variables to store that information, bools to be more precise. A way to avoid a predetermined order is with randoms values, loops and checks (“if” statements).

An approach to make this work is to create a PowerUpSpawner Class, then fill it with the information needed. Unfortunately I can’t give more advice unless you post your full script, because right now it’s a little confusing.

the variable power-up is what is storing the fastball
i just adjusted my script to
void FixedUpdate() {
if (!GameManager.instance.Playing)
return;

    NudgeIfNecessary();

    vVelocityLastFrame = rb.velocity;
    if (GameObject.FindGameObjectWithTag("Ball") == null && GameObject.FindGameObjectWithTag("secondBall") == null)
    {
        powerups[5] = powerup;
    }
    else
    {
        powerups[4] = ponit;
    }
     if (GameObject.FindGameObjectWithTag("Ball") == null && GameObject.FindGameObjectWithTag("thirdBall") == null)
    {
        powerups[4] = powerup2;
    }
    else
    {
        powerups[4] = ponit;
    }
}

void OnCollisionEnter(Collision collision)
{
if(!GameManager.instance.Playing)
{
Debug.LogWarning(“Ball OCE but not playing”);
return;
}
vDir = Vector3.Reflect(vVelocityLastFrame,
collision.contacts[0].normal);
ApplyVelocity();

    Brick b;
    if(b = collision.gameObject.GetComponent<Brick>())
    {
       Transform newExplosion =
            Instantiate(explosion, collision.transform.position, collision.transform.rotation);
        Destroy(newExplosion.gameObject, 3);
        b.HitTaken();
    }
    Bricks bs;
    if(bs = collision.gameObject.GetComponent<Bricks>())
    { 
            
          //  if (GameObject.FindGameObjectWithTag("Ball") != null || GameObject.FindGameObjectWithTag ("secondBall") !=null)
            {
                Instantiate(powerups[Random.Range(0, powerups.Length)], collision.transform.position, Quaternion.identity);
            }
     //   if (GameObject.FindGameObjectWithTag("Ball") == null && GameObject.FindGameObjectWithTag("secondBall") == null)
       // {
     //       Instantiate(powerup);
    //    }


    }
}

it seems to have fixed the problem

1 Like

Hi @sparky_robinson,

Welcome to our community and good job on challenging your self. :slight_smile:

it seems to have fixed the problem

Does that mean the problem is solved?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.