Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Omerl_ahire · Dec 28, 2017 at 06:12 AM · script.damageoncollisionenterparametershealth

Trouble setting a parameter with a collision

Hi, I'm working on health script that does script that damages different health meter depending on what gameobject is hit, unfortunately I am unable to set the gameobject in the "takeDamage" function from the "projectileBullet" script's OnCollisionEnter function.

When the bullet hits the collider the prints the "colBase" as the object it hits but the console gives me a NullReferenceException for the set colBase line(Line 18).

I don't know if it matters but the gameobjects and colliders are children to an empty gameobject.

The Bullet Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ProjectileBullet : MonoBehaviour {
     public GameObject colBase;
     [SerializeField] int damage;
     [SerializeField] GameObject hitEffect;
 
     private void OnCollisionEnter(Collision col) {
 
         ContactPoint conActual = col.contacts[0];
         Vector3 pos = conActual.point;
         Instantiate(hitEffect, pos, transform.rotation);
 
         colBase = col.gameObject;
         print(colBase);
         col.gameObject.GetComponent<ArmorScriptHumanoid>().takeDamage(damage,colBase); 
         
         Destroy(this.gameObject);
     }
 }
 

The Health Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ArmorScriptHumanoid : MonoBehaviour {
 
     ////////////Torso parts///////////
     public GameObject midTorso;
     [SerializeField] float CoreArmor;
     public GameObject leftTorso;
     [SerializeField] float leftCoreArmor;
     public GameObject rightTorso;
     [SerializeField] float rightCoreArmor;
     void Start () {        
     }
     void Update () {        
     }
     public void takeDamage(int amount,GameObject colActual) {
         
         print(colActual +"armor");
                 if ((colActual==midTorso) || (leftCoreArmor <=0) || (rightCoreArmor <=0)) {
                         CoreArmor -= amount;
                 return;
             }
 
         if (colActual == leftTorso) {
             leftCoreArmor -= amount;
             print("Left");
         }
         if (colActual == rightTorso) {
             rightCoreArmor -= amount;
             print("Right");
         }
     }
 }
 
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 Elisvaldo · Dec 28, 2017 at 12:35 PM 0
Share

Have you double checked if your target has the ArmorScriptHumanoid component? The null reference can come from the GetComponent() and not from the colBase.

1 Reply

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

Answer by niocy · Dec 28, 2017 at 12:46 PM

ArmorScriptHumanoid aScript;

If((aScript = col.gameObject.GetComponent(ArmorHumanoidScript)) != null){ Do stuff }

It might hit something that doesnt have that script so check it

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 Omerl_ahire · Dec 28, 2017 at 08:27 PM 0
Share

Thanks for your help I changed ` col.gameObject.GetComponent().takeDamage(damage,colBase); To

 col.gameObject.transform.parent.GetComponent().takeDamage(damage,colBase); 

And now it works.

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

75 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

Related Questions

health not subtracting 1 Answer

Health Regeneration 2 Answers

Health bar losing health 1 Answer

Damage/Health problem 2 Answers

Weapon Mesh Damage 1 Answer


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