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 /
avatar image
0
Question by AryaIsMe · Nov 19, 2021 at 06:22 PM · 3ddamage

Why is my goblin not taking damage

I am trying to get my goblin to take damage and I made a script for that however it isn't working. Can you please take a look at it? I will show the entire script but only the bottom part is relevent.

Edit: I forgot to add that when I pass the weapon through the health value doesn't change Edit 2: also the weapon has the weapon tag and both objects have the isTrigger enabled

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class Enemy
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     
 
     public Enemy(float health, float speed, NavMeshAgent agent, float inRange)
     {
         this.health = health;
         this.speed = speed;
         this.agent = agent;
         this.inRange = inRange;
     }
 
 }
 
 public class EnemyBaseScript : MonoBehaviour
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     public Enemy enemy;
     
     // Start is called before the first frame update
     void Start()
     {
         enemy = new Enemy(health, speed, agent, inRange);
         agent = gameObject.GetComponent<NavMeshAgent>();
     }
 
     public void Update()
     {
         Invoke("LookForPlayer", 5.0f);
     }
        
 
     public GameObject playerPos;
 
 
 
     public void LookForPlayer()
     {
         playerPos = GameObject.FindGameObjectWithTag("Player");
     }
 
     public bool FindPlayerPos(float inRange)
     {
         if ((playerPos.transform.position).sqrMagnitude >= inRange)
         {
             return true;
         }
 
         return false;
     }
 
     public void ChasePlayer(NavMeshAgent agent)
     {
 
     }
 
     private Weapon weaponGettingHitBy;
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Weapon")
         {
             weaponGettingHitBy.item = col.gameObject;
             weaponGettingHitBy.damagedelt = col.gameObject.GetComponent<WeaponBaseScript>().damagedealing;
             TakeDamage(health, weaponGettingHitBy); //change back to enemy.health
         }
         
     }
 
     public float TakeDamage(float h, Weapon AttackingItem)
     {
         h = h - AttackingItem.damagedelt;
         return h;
     }
 }
 
 

This is the script for the weapon:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Weapon
 {
     public GameObject item;
     public float damagedelt;
 
     public Weapon(GameObject item, float damagedelt)
     {
         this.item = item;
         this.damagedelt = damagedelt;
     }
 }
 
 public class WeaponBaseScript : MonoBehaviour
 {
     public GameObject self;
     public float damagedealing;
     private Weapon weapon;
 
 
     // Start is called before the first frame update
     void Start()
     {
         weapon = new Weapon(self, damagedealing);
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 
 
 






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

1 Reply

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

Answer by Eno-Khaon · Nov 19, 2021 at 07:35 PM

You're getting a new value for health, but doing nothing with it:

 TakeDamage(health, weaponGettingHitBy);
 // ...
 public float TakeDamage(float h, Weapon AttackingItem)
 {
     h = h - AttackingItem.damagedelt;
     return h;
 }


You just need to apply that updated health value to the "Enemy" now:

 health = TakeDamage(health, weaponGettingHitBy);


Alternatively, you can use your "health" variable as-is in the TakeDamage() function instead of passing it in, or you could even pass health in by reference to keep the function the way it is:
 TakeDamage(ref health, weaponGettingHitBy);
 // ...
 public void TakeDamage(ref float h, Weapon AttackingItem)
 {
     h = h - AttackingItem.damagedelt;
 }
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 AryaIsMe · Nov 19, 2021 at 09:20 PM 0
Share

I also needed to add a rigidbody to the object after this but then it worked thanks.

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

170 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

Related Questions

How to make enemys do damage and you dying etc 1 Answer

Damage dealt over time question, Need Help 1 Answer

Jumping not always work 2 Answers

Addjustcurrenthealth not working 0 Answers

How can I import 3D .obj files that use Unity's gravity when it's imported (a bunch of separate 3D rigidbodies)? 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