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 /
  • Help Room /
avatar image
0
Question by Typods · Jan 25, 2021 at 01:29 AM · triggerdamageaccessing scriptshealth

Identify Variable from Triggering Object

Hi! So I'm writing a script on an enemy to detect a hit from a damaging ability and I want to be able to decide how much damage the ability will cause with a float on a script attached to the ability. I want to make this more accessible and changeable for future damaging abilities I add. So, on the damaging object, is a script named damageAmount which contains nothing important except the two floats:

 public float Damage;
 public float DPS;

I want to be able to use this script on all of my future damaging abilities, adjusting the numbers for each in the future.

Now, the script on the enemy is as follows:

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

 public class DummyHealth : MonoBehaviour
 {

 public float Health;
 public float DummyDamagePerSecond;
 public float DummyDamageTaken;
 

 // Update is called once per frame
 void Update()
 {
    /// DummyDamageTaken = col.gameObject.GetComponent<damageAmount>().Damage;   <- I know this is incorrect
 }

   
 void OnTriggerStay2D(Collider2D col)
     {
         if (col.gameObject.tag == "DamagingEnemies")
         {
             artCoroutine(InflictDamage());
         }
     }


 IEnumerator InflictDamage()
 {
     Debug.Log("DamageInflicting...");

     Health -= DummyDamageTaken;

     Debug.Log("Damage Inflicted");

     yield return new WaitForSeconds(DummyDamagePerSecond);
 }
 }


The part I'm having trouble with is

 /// DummyDamageTaken = col.gameObject.GetComponent<damageAmount>().Damage;


My goal is to assign the variable 'DummyDamageTaken' to the number that is attached to the Damage variable on the script on the colliding object.

Again, I would like to have the same script on multiple different objects, and then the enemy will identify which object is hitting so it can pull the number from that specific script.

I'm sorry if this is so confusing and complicated, I'm fairly new to programming and am doing my best to learn. If you need me to clarify, feel free to ask!

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
0

Answer by mattanmohel · Jan 25, 2021 at 01:58 AM

Hello. If it were me, I would make the health/damage-dealing components a lot more generic, as each object can have damage dealing and health class and a custom way to trigger the damage dealing. From there you can pass in the damage value as a parameter to the health component of the target, where that value will be subtracted from the current health. In terms of creating custom behavior when taking damage, you could create an event delegate that will be called whenever an entity takes damage, and then add your custom damage-dealing behavior to the delegate through the custom entity script. Overall I would recommend separating the functionality over several classes and making each class as generic as possible so it can be reused between objects.

Comment
Add comment · Show 2 · 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 Typods · Jan 25, 2021 at 02:30 AM 0
Share

Hi, thank you so much for the helpful feedback! I've never heard of classes or event delegates before, could you point me in the direction of some websites or videos I can use to learn about it?

avatar image mattanmohel Typods · Jan 25, 2021 at 02:44 AM 0
Share

Yeah, delegates are pretty much variables that can store methods in them, and can call those same methods at a later time. Here are two videos regarding the topic: https://www.youtube.com/watch?v=OuZrhykVytg&t=363s & https://www.youtube.com/watch?v=3ZfwqWl-YI0&t=154s

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

176 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

Related Questions

Can't get health/damage to work 0 Answers

Inconsistent 2d collider hit registration 3 Answers

can't damage spawned prefab 1 Answer

How do i deal damage with raycasts in UNET? 0 Answers

An object reference is required to access non-static member 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