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 Fred_1996 · May 01, 2014 at 11:34 AM · 2dcollisionerrorcollidertrigger

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.

Comment
Add comment · Show 5
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 Yword · May 02, 2014 at 03:11 AM 0
Share

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.

avatar image Fred_1996 · May 03, 2014 at 09:40 AM 0
Share

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.

avatar image Pyrian · May 03, 2014 at 01:41 PM 0
Share

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.

avatar image Fred_1996 · May 03, 2014 at 07:49 PM 0
Share

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);
         }
     }
avatar image Jeff-Kesselman · May 03, 2014 at 07:53 PM 0
Share

Object that are flagged as disabled when the game starts never get made.

0 Replies

· Add your reply
  • Sort: 

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

23 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

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


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