Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 danny_appel · Jan 16, 2021 at 02:24 PM · collisionscript.classnullaccess

Can't access script through collision.

Hi all,

I'm having a problem that I cant solve for literally days. I have a bullet with a script that when it collides with an enemy with the tag "Enemy", it should get the DealDamage from that gameobject (the enemy) with the EnemyHealth script.

I've done this before in the past but when im doing the same thing now, I keeps giving me a "NullReferenceException: Object reference not set to an instance of an object" error. I think it should mean that it cannot locate the script that its searching but its not there... but it is? Its giving me a headache!

Please help me :(

It gives me the error on line 38.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 
     public class PlayerAmmoAssaultRifle : MonoBehaviour
     {
         PlayerStats playerStats;
 
         private float BulletCount = 30;                         // Each bullet does the damage of 1 / 30 of total damage.
         private float Damage;
         private float CritChance;
         private float CritChanceRoll;
         private float CritChanceMin = 0;
         private float CritChanceMax = 101;
 
         public GameObject bloodSplatter;
        // EnemyHealth enemyHealth;
 
 
 
         void Awake()
         {
             playerStats = GameObject.FindObjectOfType<PlayerStats>();
             Damage = playerStats.damage / BulletCount;
             CritChance = playerStats.playerTotalCritChance;
             Destroy(gameObject, 3f);
         }
 
 
         private void OnCollisionEnter(Collision collision)
     {
         
             if (collision.gameObject.CompareTag("Enemy"))
             {
                 collision.gameObject.GetComponent<EnemyHealth>().DealDamage(Damage);
                 
 
                 Instantiate(bloodSplatter, this.transform.position, this.transform.rotation);
                 CritChanceRoll = (Random.Range(CritChanceMin, CritChanceMax));
                 if(CritChanceRoll <= CritChance)
                 {
                     Damage *= 2;
                     Debug.Log("Critical Damage of = " + Damage + " !!!");
                 }
                 else
                 {
                     Debug.Log(Damage);
                     Destroy(gameObject);
                 }
             
             }
             else if(collision.gameObject)
             {
                 Destroy(gameObject);
             }
         }
     }


using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class EnemyHealth : MonoBehaviour { public float health; public float maxHealth;

 public GameObject healthBarUI;
 public Slider slider;
 private string enemyTitle;
 public Text enemyTitleText;
 LootDrop lootDrop;
 EnemyStats enemyStats;

 

 Transform player;

 private void Start()
 {

     enemyStats = GetComponent<EnemyStats>();
     health = maxHealth;
     slider.value = CalculateHealth();
     player = GameObject.FindGameObjectWithTag("Player").transform;
     enemyTitle = enemyStats.enemyName.ToString();
     enemyTitleText.text = enemyTitle.ToString();
     lootDrop = GetComponent<LootDrop>();
      
 }

 private void Update()
 {
     slider.transform.LookAt(player);
     enemyTitleText.transform.LookAt(2 * enemyTitleText.transform.position - player.transform.position);   // (2 * enemyTitleText.transform.position - player.transform.position)       <<< this makes the Text go Backwards!!!!!
     slider.value = CalculateHealth();

     if(health < maxHealth)
     {
         healthBarUI.SetActive(true);
     }

     if(health <= 0)
     {
         lootDrop.OnDeath();
         Destroy(gameObject);
     }

     if(health >= maxHealth)
     {
         health = maxHealth;
     }
 }

 float CalculateHealth()
 {
     return health / maxHealth;
 }

 public void DealDamage(float damage)
 {
     health -= damage;
 }

}

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 Llama_w_2Ls · Jan 16, 2021 at 02:49 PM 0
Share

Have you tried collision.collider.gameObject instead of just collision.gameObject? Also, the enemy might not have an EnemyHealth script, but this in unlikely. @danny_appel

avatar image danny_appel Llama_w_2Ls · Jan 16, 2021 at 02:55 PM 0
Share

Unfortunately it gives me the same error :(

And yea, the enemy has the script, I can even chance the health in the inspector while testing. I seriously dont understand why its not working.

Ofcourse it works when im using FindObjectOfType but that doesnt work while having more then 1 enemy.

avatar image Duckocide · Jan 16, 2021 at 03:08 PM 0
Share

Guessing - There may be other objects with tag "Enemy" that don't have the dealdamage script? (is that what is null). $$anonymous$$gestion - $$anonymous$$ay be debug and see what collision object contains. $$anonymous$$gestion - Retrieve the script instance in to a variable and break point when that is null? Idea - $$anonymous$$ay be place the collision processing on the Enemy, you can then check they are hit by all sorts of things (not just bullets).

avatar image danny_appel Duckocide · Jan 16, 2021 at 03:26 PM 0
Share

You were right, one object didnt had the script but did had the tag. Though this didnt solve it. The thing that is null when colliding, is the EnemyHealth script, eventhough it has the script.

I tried Debug.Log(collision.gameObject) and it gives back the enemy gameobject. But when I tried to do Debug.Log(collision.gameObject.GetComponent() it gives me "Null".

And about your idea, I was thinking that aswell but im so tunnelvisioned about this problem, because I dont see why its not working, as it clearly should, right?

If all else fails, I will do it on the EnemyHealth script.

avatar image danny_appel danny_appel · Jan 16, 2021 at 03:27 PM 0
Share
  • Debug.Log(collision.gameObject.GetComponent())

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by danny_appel · Jan 16, 2021 at 03:53 PM

I found out the problem... My enemy had a Mesh Collider on a child... It didnt even had a collider on the parent! Im so dumb lol. Though when im placing a Mesh Collider on the parent it doesnt work either, so I placed a spherical collider and it works now. And yes, I was stuck for a few days because of this dumb problem.

Thanks all for your input and help!

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
avatar image
1

Answer by logicandchaos · Jan 16, 2021 at 03:39 PM

Make sure your object is set up correctly. They way it's coded your EnemyHealth script should be on the gameObject that is colliding, if it's attached to a child object GetComponentInChildren. Try adding this debug line 35 Debug.Log(collision.gameObject); and Debug.Log(collision.gameObject.GetComponent()); That should let you know if you are getting the right object.

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 danny_appel · Jan 16, 2021 at 03:53 PM 1
Share

Thanks! This made me look into the child/parent relation with colliders and found the problem!

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

207 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

Related Questions

Why are objects just set to null? 2 Answers

C# Inheritance, base class attributes, override and null object 1 Answer

transform is destroyed (ERROR)... and I want the game to complete 1 Answer

Accessing another script from a class 3 Answers

Null Collision Detection 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