Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rejwel · Oct 08, 2020 at 03:04 PM · gameobject2d gamecoroutinefunction

How can I track if GameObject is null in function?

Hello, I'm currently working on a 2D Game and I have a problem. I've created a special void functions about guns, and one of them is Bazooka. I want to fire a bullet, then track if the bullet hit a collider, if no, just wait a 1,5 seconds then blow up, but at the same time if the bullet hit collider I want Boom aswell. So I thought about it, and I think it might be something with Coroutines, but I can't apply it.

This is my function

 IEnumerator FireBazooka()
     {
         var bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
         
         yield return new WaitForSeconds(2f);
         
         for (float i = 0; i < 10; i++)
         {
             Instantiate(bulletPrefab, bullet.transform.position, 
 firePoint.transform.rotation * quaternion.Euler(new Vector3(0, 0, i + Random.Range(1f, 10f))));
         }
         Destroy(bullet);
     }

So it obviously wait, and then explode, but if the bullet is null it won't explode. how Could I track this bullet if its going to be null?

Probably there is another way to solve this, so I highly appreciate your suggestions :)

Thanks everybody!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by theLameBrain · Oct 08, 2020 at 03:10 PM

I did something similar in a game I made, I do not know if it is the best solution, but I basically created a bullet prefab, then put a collider and a script on the prefab.

The script did two things: 1. It watched for collisions 2. It increased a counter float in the Update() function.

Then, if it collided with something or the counter reached a certain threshold (which I exposed as a public value so I could edit in the inspector) it spawned an explosion and removed the bullet.

Comment
Add comment · Show 1 · 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 Rejwel · Oct 08, 2020 at 04:28 PM 0
Share

Hey, thank you for answer, I'll try to implement this to my project and let you know if it works in my situation :)

avatar image
1

Answer by Klarzahs · Oct 08, 2020 at 05:16 PM

Have a look at your console output (lower window): If the bullet is null after the 2 seconds, the Instantiate(..., bullet.transform.position, ...); is accessing a null pointer, throwing an exception and halting all code. As the "explosion" would come afterwards your bullet doesn't explode.
If you want to detect the null condition, just check for if(bullet == null), which works for any GameObject.

Apart from that, the answer by theLameBrain in regards to a collider is correct. You'll need colliders on any object that it can explode on, though. Have a look at this documentation.
You can increase a float value by Time.deltaTime in your Update() function, which stores how many seconds ago the Update call was executed last. This isn't as beautiful as your coroutine, but it works in most cases just as well and is easier to understand.

Comment
Add comment · Show 2 · 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 Rejwel · Oct 08, 2020 at 07:33 PM 0
Share

Yes exactly, but the yield return new WaitForSeconds(2f); is stopping this if statement, so the detection is working only after the yield, and this is not what I want.

avatar image Klarzahs Rejwel · Oct 08, 2020 at 09:24 PM 0
Share

Why not? Sure, its not the greatest solution, but its very probably (:D) not bad for performance. Otherwise you can do something like this:`while(notNull){ notNull = (bullet != null); yield return new WaitForSeconds(0.3); }`

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

255 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 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

Hi I am making a 2D game and I am having problem with this script: 1 Answer

My function isn't ending but is being repeatedly being called from the update function 1 Answer

WaitForSeconds is not waiting for seconds, at all. Its like it doesnt exist. 1 Answer

Static and Coroutines help 2 Answers

Dynamic function calling 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