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 /
  • Help Room /
This question was closed Oct 05, 2019 at 11:36 AM by tormentoarmagedoom for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by smarjanovicng · Oct 05, 2019 at 08:56 AM · projectiledamagegun script

How to deal damage to only enemy hit?

I have a prefab enemy that naturaly has a float healthPoints, when HP gets to 0, the enemy dies. Now for my project I have to use projectiles instead of raycasting on my guns, the damage script is laying on my bullets. The problem is when i hit one enemy, all enemys get damaged. I know whats wrong here, but i dont know the right solutions, can anybody provide some insight? Here is the damage script. My question is how to get the enemy script that has been shot.

public class DealDamage : MonoBehaviour { public Transform explode; public float dmg;

 private EnemyScript enemyScript;
 private Shooting shooting;

 void Awake()
 {
     shooting = FindObjectOfType<Shooting>();
     enemyScript = FindObjectOfType<EnemyScript>();
     dmg = shooting.gun_dmg;
 }

 void OnTriggerEnter(Collider collision)
 {
         if (collision.gameObject.tag == "Enemy")
         {

         enemyScript.TakeDmg(dmg);
         }

     Debug.LogWarning("Enemy HIT!");
     }
 }

THIS IS FROM THE ENEMY SCRIPT public void TakeDmg(float dmgAmount) { healthPoints = healthPoints - dmgAmount; if (healthPoints < 0f) { Die(); } }

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1

Answer by Mokomaki · Oct 05, 2019 at 09:26 AM

Ok so you are you are using

 FindObjectOfType<EnemyScript>();

To get the enemy and this will just find the first object in the scene not the one you shot. You can actually use OnTriggerEnter Collider to get the reference like this:

 collision.gameObject.GetComponent<EnemyScript>().TakeDmg(dmg);

Hope this helps :)

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
0

Answer by smarjanovicng · Oct 05, 2019 at 11:28 AM

 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         other.gameObject.GetComponent<EnemyScript>().TakeDmg(dmg);
     }
 }

Now i get the msg: NullReferenceException: Object reference not set to an instance of an object DealDamage.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Player/DealDamage.cs:23)

This script is attached to the projectile. Thank you!

Comment
Add comment · Show 3 · 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 tormentoarmagedoom · Oct 05, 2019 at 11:36 AM 0
Share

$$anonymous$$an, go look for this error in google. spend your time trying reading trying trying and trying. And learn from experience.

avatar image smarjanovicng tormentoarmagedoom · Oct 05, 2019 at 11:37 AM 0
Share

$$anonymous$$an, what are you talking about, dont you think I've already read trough different types of problems? I'm doing something wrong and i asked for help, no need to be a dick about it.

avatar image Mokomaki · Oct 05, 2019 at 01:31 PM 0
Share

I think the problem is with the EnemyScript i made a simple copy of what you have here and it worked fine.

Follow this Question

Answers Answers and Comments

183 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

Related Questions

Basic damage script is not working 0 Answers

How do i get my bullets to do damage?,i have a player than can shoot bullets but i don't know how to make the bullets do damage and go after. Can anyone help? 0 Answers

Best practice where to store Projectile Damage amounts? 1 Answer

How to make my snowball model to actually shoot like a projectile? 0 Answers

How to make my gun do damge and destroy a object? 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