Only execute a Function if something is passed in?

Language: C#

Goal: Only call a function if the object being returned to it is NOT null.

Current Progress: I’m setting up Object Pooling with my PlayerWeaponFire script. The ObjectPool script has a Activate() function that returns the current GameObject its searching for in its for loop.

	public GameObject ActivateObject()
	{
		for (int i = 0; i < poolSize; i++)
		{
			if (objects*.activeInHierarchy == false)*
  •  	{*
    

_ objects*.SetActive(true);_
_
avaiableObject = true;_
_ return objects;
}
}
return null;
}*
My PlayerWeaponFire script then calls this ActivateObject() function to enable a bullet in the ObjectPool._

* IEnumerator FirePrimary ()*
* {*
* ObjectStats playerStats = GetComponent();*

* if (Time.time > nextPrimaryFire)*
* {*
* nextPrimaryFire = Time.time + primaryFireRate;*
* {*
* for(int i = 0 ; i < primaryNumberOfShots; i++)*
* {*
* Activate(primaryPool);*
* playerStats.currentAmmo = playerStats.currentAmmo - primaryAmmoCost;*
* yield return new WaitForSeconds (primaryShotInterval);*

* }*

* }*
* } *
* }*
Need: I need to be able to skip over the Activate() function if the GameObject being returned to it is null.

The common practice to check if a GameObject reference is null is simply

if (gameObject){
    //Only execute if a gameObject is not null
}

if (!gameObject){
    //Only execute if gameObject is null
}

You can also do this on a method call that returns a GameObject. In your example

if(Activate(primaryPool)){
    //Do stuff that needs this function to not return null
}

If you need the result from Activate(primaryPool) you could use this

GameObject primaryPoolResult = Activate(primaryPool)
if(primaryPoolResult)){
    //Do stuff that needs this function to not return null
    //primaryPoolResult will contain the GameObject returned by your method
}