- Home /
Collision returning an error
Hi there, I have come across an problem that I can't solve.
This is part of my crate's script. When in collides, I want it to spawn a coin from the pool.
No errors show up in the console until the "rocket" (basically a bullet) collides with the box. I also have a rocket pool and all the rockets inside the hierarchy are clones of the original one. This is a 2D platformer/run'n'gun game.
void OnCollisionEnter2D(Collision2D collision)
{
//Prepare coin spawn from pool and set to crate position
GameObject obj = CoinPooling.current.GetPooledObject();
if (obj == null) return;
obj.transform.position = transform.position;
if(collision.gameObject.tag == "Player") //If my player hits the crate spawn coin and set crate inactive
{
obj.SetActive(true);
gameObject.SetActive(false);
}
else if (collision.gameObject.tag == "Rocket")//Error seems to be here.
//Spawn coin, deactivate crate, activate explosion particle system (attached to rocket), then deactivate rocket.
{
obj.SetActive(true);
gameObject.SetActive(false);
collision.transform.FindChild("Explosion").gameObject.SetActive(true); //Trigger explosion
collision.gameObject.SetActive(false);
}
}
Do you have any idea why the error message is : Object Reference Not Set To An Instance Of An Object ?
Thank you in advance.
Can you specify which line of code has error? I assume that "collision.transform.FindChild("Explosion")" is returning null, maybe the collided object does not has a child named "Explosion", or the child named "Explosion" is not active.
Hi there, I double checked and all the game objects tagged "Rocket" have an Explosion game object as a child. However the Explosion game object is inactive. That is why I want to activate once before destroying adding the Rocket back into the pool. The line of code returning null is this one : else if (collision.gameObject.tag == "Rocket")
$$anonymous$$aybe I should create an "Explosion" pool and activate an explosion when needed ?
It is probably the way I coded this that is inefficient.
I don't know why you're getting an error, but I can tell you that activating a child GameObject and then deactivating its parent results in an inactive child. If you inactivate a GameObject, all its children are effectively inactive, as well, regardless of their own active status.
Hello, Pyrian thank you for your answer.
I actually solved the problem by creating an "explosion" pool and making explosions "independent". This improves performance, and it is easier to understand.
This is my new code :
void OnCollisionEnter2D(Collision2D collision)
{
//Prepare coin spawn at crate position
GameObject obj = CoinPooling.current.GetPooledObject();
if (obj == null) return;
obj.transform.position = transform.position;
GameObject exp = ExplosionsPool.current$$anonymous$$GetPooledObject();
if(exp == null) return;
if(collision.gameObject.name == "Player")
{
//Add 400 points to my overall score.
Score$$anonymous$$eeper.score += 400;
//Spawn coin
obj.SetActive(true);
//Deactivate crate
gameObject.SetActive(false);
}
else if (collision.gameObject.tag == "Rocket")
{
Score$$anonymous$$eeper.score += 400;
//Spawn coin
obj.SetActive(true);
//Trigger explosion
exp.transform.position = transform.position;
exp.SetActive(true);
//Set Rocket inactive
collision.gameObject.SetActive(false);
//Deactivate crate
gameObject.SetActive(false);
}
}
Object that are flagged as disabled when the game starts never get made.
Your answer
Follow this Question
Related Questions
How to use Physics2D.IgnoreCollision2D in Unity 5 to cancel a trigger 0 Answers
Detect a collision point, but allow pass through collider. 0 Answers
Cirlce Colliders 0 Answers
Collider2D/RigidBody2D not working 1 Answer
Collision with renderer.enabled? 0 Answers