- Home /
GameObject return if, else
I am having difficulties creating a GameObject class, basically what I want is for unity to stop throwing errors when the powerUpPrefabs array is empty and ideally print out a debug.log saying that the array is empty.
private GameObject RandomPowerUp()
{
if (powerUpPrefabs.Length != 0)
{
return powerUpPrefabs[Random.Range(0, powerUpPrefabs.Length)];
}
else
{
//what do I type here so it doesn't throw an error?
}
}
What I am struggling with is figuring out what to type in the else statement, because it requires a GameObject to be return which neither the debug.log nor null is.
you have to return null. if you want to return a default object create one GameObject default and return that one but it doesnt makes much sense for your case.
If I return null then the console still throws errors everytime the class is called:
thats because you are trying to instantiate the object when is null, when you should simply not instantiate anything
GameObject myObject = RandomPowerUp();
if(myObject != null) { Instantiate(myObject ); }
Answer by Robertelis · Mar 18, 2019 at 10:42 AM
I figured out the message @xxmariofer were trying to convey and added this snippet of code where the main function is called:
{
DestroyBlock();
if (thePowerUp.powerUpPrefabs.Length != 0)
{
thePowerUp.SpawnRandomPowerUp().transform.position = this.transform.position;
}
else
{
Debug.Log("No PowerUps loaded");
}
}
This also requires a lot less nulls in the PowerUp function, so now the code looks cleaner:
public class PowerUpSpawn : MonoBehaviour
{
int powerUp = 1;
[SerializeField] int powerUpChance;
public GameObject[] powerUpPrefabs;
public GameObject SpawnRandomPowerUp()
{
if (powerUp == Random.Range(1, powerUpChance))
{
return Instantiate(RandomPowerUp(), transform.position, transform.rotation);
}
else
{
return null;
}
}
private GameObject RandomPowerUp()
{
return powerUpPrefabs[Random.Range(0, powerUpPrefabs.Length)];
}
}
Answer by Orami · Mar 18, 2019 at 10:34 AM
It depends on your code I would think NULL just check for that before you go instantiating something.... or check that you have something in this array before calling this function and you could use if(powerUpPrefabs.Length > 1){//code}else return powerUpPrefabs[0];
Answer by xxmariofer · Mar 18, 2019 at 10:35 AM
this should be a bit easier to read
public GameObject SpawnRandomPowerUp()
{
if (powerUp != Random.Range(1, powerUpChance))
{
return null;
}
GameObject powerUpOObject = RandomPowerUp();
if (powerUpOObject == null)
{
return null;
}
return Instantiate(RandomPowerUp(), transform.position, transform.rotation);
}
private GameObject RandomPowerUp()
{
if (powerUpPrefabs.Length == 0)
{
return null;
}
return powerUpPrefabs[Random.Range(0, powerUpPrefabs.Length)];
}
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Using a lot of calls and if else statement, is there a better way to do this? 1 Answer
Reference a instantiated object? 2 Answers
Moving an object based on position of other object 1 Answer
Access a child from the parent or other gameObject. 2 Answers