Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Robertelis · Mar 18, 2019 at 10:01 AM · gameobjectif-else

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Mar 18, 2019 at 10:06 AM 0
Share

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.

avatar image Robertelis xxmariofer · Mar 18, 2019 at 10:11 AM 0
Share

If I return null then the console still throws errors everytime the class is called: alt text

untitled.png (7.2 kB)
avatar image xxmariofer Robertelis · Mar 18, 2019 at 10:15 AM 0
Share

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 ); }
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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)];
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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];

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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)];
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges