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 /
This question was closed Oct 08, 2014 at 06:30 AM by Kiwasi for the following reason:

The question is answered

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by PlayKiseki · Oct 06, 2014 at 12:05 AM · bulletsshmuphealth

Inconsistent script results/error

Hi there,

I'm working on a simple health script for my game. It should be simple- if the object in which healthscript.cs is attached gets hit by a player bullet, it takes damage. When its HP = 0, it should return to the pool (a.k.a. be destroyed).

It seems like the script works every 2 out of 10 times: The bullet hits the enemy, which is destroyed and returned to the pool. All the other times, the enemy absorbs bullets like a sponge and the console throws this error every time the shot hits:

NullReferenceException: Object reference not set to an instance of an object healthscript.OnTriggerEnter2D (UnityEngine.Collider2D otherCollider) (at Assets/Scripts/healthscript.cs:22)

Here is my health script, where the magic (and errors) happen:

 using UnityEngine;
 using System.Collections;
 
 public class healthscript : MonoBehaviour
 {
 
         public int health = 1;
         public bool isEnemy = true;
 
         public void Damage (int damageCount)
         {
                 health -= damageCount;
                 if (health <= 0) {
                         GameObject obj = explodepool.current.GetPooledObject ();
                         obj.transform.position = transform.position;
                         obj.transform.rotation = transform.rotation;
                         obj.SetActive (true);
 
                         gameObject.SetActive (false);
                 }
         }
 
         void OnTriggerEnter2D (Collider2D otherCollider)
         {
                 playerbulletscript shot = otherCollider.gameObject.GetComponent<playerbulletscript> ();
                 if (shot != null) {
                         if (shot.isEnemyShot != isEnemy) {
                                 Damage (shot.damage);
                         }
                 }
         }
 }
Comment
Add comment · Show 1
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 KayelGee · Oct 06, 2014 at 11:21 AM 0
Share

Is it possible that you are destroying the shot when it enters the Trigger in another place? If this is the case the otherCollider could be null because the GameObject it belongs to got destroyed.

Unless you're using DestroyImmediate I would guess that Unity sometimes destroys the GameObject of the shot before the TriggerEnter gets called and sometimes afterwards. Which would explain the random behaviour you are experiencing.

If this is the case then a solution would be to destroy your shots on the next Frame and not on the current.

1 Reply

  • Sort: 
avatar image
0

Answer by PlayKiseki · Oct 07, 2014 at 11:57 PM

Hi KayelGee,

I figured out what the issue was. There were two scripts destroying the object at once, caused my bulletscript to try and deactivate itself after it had been activated. Thanks anyway!

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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2.5D Shmup, how to create bullets on "camera plane"? 0 Answers

Health System Raycast Bullets 2 Answers

bullets that do damage 1 Answer

Making an HUD 3 Answers

How to: Do a camera that is top Down/Isometric 5 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