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 MithilaG · Jul 28, 2019 at 04:17 AM · scripting problemraycastenemydamage

Enemy and gun damge script not working.

I have a enemy script and a gun damage script which uses raycasting but my enemy wont take damage and loose health can someone please check whats wrong with this script. Enemy script

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

 public class EnemyScript : MonoBehaviour
 {
     public int EnemyHealth = 10;

 void deductPoints(int DamageAmount)
 {
     EnemyHealth -= DamageAmount;
 }

 void Update()
 {
     if (EnemyHealth <= 0)
     {
         Destroy(gameObject);
     }
 }

}

Gun damage script

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
 
 public class HandGunDamage : MonoBehaviour
 {
 public int DamageAmount = 5;
 public float TargetDistance;
 public float AllowedRange = 15.0f;


     void Update()
    {
     if (Input.GetButtonDown("Fire1"))
     {
         RaycastHit Shot;
         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
         {
             TargetDistance = Shot.distance;
             if (TargetDistance < AllowedRange)
             {
                 Shot.transform.SendMessage("DeductPoints", DamageAmount);
             }
         }
     }
 }

} Please help me i have been searching a solution of this for some time now but i cant find a fix for this and i know there was a similiar post like this but i didnt help me. Thanks in advance.

,

Comment
Add comment · Show 2
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 Divinitize1 · Jul 28, 2019 at 04:49 AM 0
Share

I'm still a beginner myself when it comes to rays, so ill add a comment rather than an answer. I can't see anywhere you're even trying to communicate with the enemy?

Found this on a post from 2013, so not sure if it's relevent but it might help you.

 shot.collider.gameObject.GetComponent<EnemyScript >().DeductPoints(1);

$$anonymous$$ight also be useful to use something like

 if (shot.transform.tag=="Enemy"


first. Best of luck

avatar image MithilaG Divinitize1 · Jul 28, 2019 at 06:30 AM 0
Share

actually communication is happening because in the hierachy i attached the health script to the enemy and i attached the gun damage script to a empty object which i attached to the player

2 Replies

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

Answer by Hellium · Jul 28, 2019 at 07:55 AM

Divinitize1 is right, you have a big flaw in how you call your function on the enemy. SendMessage should be called only when prototyping.


Instead, you should simply check whether the hit gameObject (or one of its children) has the desired component and having a tag on the enemy will reduce unnecessary calls to GetComponent.


So tag your enemies gameObjects (and all their children) Enemy and change your weapon code to:

 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
      {
          RaycastHit shot;
          bool hittingEnemy = Physics.Raycast(transform.position, transform.forward, out shot) &&
              shot.distance < AllowedRange &&
              shot.transform.CompareTag("Enemy") ;
          if (hittingEnemy)
          {
              EnemyScript enemy = GetEnemyScript( shot.transform );
         
              if (enemy != null)
              {
                  enemy.DeductPoints(DamageAmount);
              }
              else
              {
                   Debug.LogError("An enemy does not have the EnemyScript component attached");
              }
          }
      }
 }

 private EnemyScript GetEnemyScript( Transform hitObject )
 {
     EnemyScript enemy = hitObject.GetComponentInChildren<EnemyScript>();
     while( enemy == null && hitObject.CompareTag("Enemy"))
     {
          hitObject = hitObject.parent;
          enemy = hitObject.GetComponent<EnemyScript>();
     }
     return enemy;
 }


Comment
Add comment · Show 5 · 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 MithilaG · Jul 28, 2019 at 11:00 AM 0
Share

@Hellium Thank you for replying can u please tell me where I should insert this code or whether I should replace And should I attach the script to the enemy in the hierarchy. Please help.

avatar image MithilaG · Jul 28, 2019 at 11:00 AM 0
Share

@Hellium I typed out this script but a errors pop up. can u please check on this. EnemyScript.DeductPoints(int)' is inaccesible due to its protection level

avatar image Hellium · Jul 28, 2019 at 08:03 PM 0
Share

As I have indicated, this code must replace your current Update method of your HandGunDamage script.


About your error, add public before void deductPoints(int DamageAmount) un your enemy script.

avatar image MithilaG Hellium · Jul 29, 2019 at 02:43 PM 0
Share

@Hellium when i type public void deductPoints(int DamageAmount); another error pops up the modifier public is not valid for this term. btw thanks for helping me out, I really want to make this game.

avatar image Hellium MithilaG · Jul 29, 2019 at 08:33 PM 0
Share
 public int EnemyHealth = 10;
 
 public void deductPoints(int DamageAmount)
  {
      EnemyHealth -= DamageAmount;
  }
avatar image
0

Answer by leminh_2014 · Jul 28, 2019 at 11:54 AM

let you try with event system? i think that might good choice in this case.

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

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

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

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

pick up item script issue 3 Answers

Bullethole doesn't instantiate 1 Answer

in rewarded ad which code means user closed the ad by itself 2 Answers

Raycast not detecting anything if MainCamera's X rotation is > 41.5 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