- Home /
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!
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.
Hey, thank you for answer, I'll try to implement this to my project and let you know if it works in my situation :)
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.
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.
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
Follow this Question
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