Adressing specific collicion object

For the Realm Rush project I am working on adding different towers. For that I had to rework my damage system a bit so that I can introduce variables in it. As a start I tried to add a script to my projectile with a damage vallue that should be dealt to the enemy. This kinda worked… because there are multiple enemies things became a little bit weird. So I tried to take another approach and added something to my enemy instead that reads the dmg value of the projectile.

for that I wrote this:

‘’’
{

Damage damage;

EnemyHealth enemyHealth;

void Start()

{

    enemyHealth = FindObjectOfType<EnemyHealth>();

}

void OnParticleCollision(GameObject other)

{

    Damage damage = FindObjectOfType<Damage>();

    enemyHealth.ProcessDamage(damage.amountOfDamage);    

}

}

‘’’

with a serialized value on the projectile. It works !!
for now, and i realise that. Because i know as soon as i will introduce different damage scripts and values it will go wrong again. I know that i can fix this in the easy way of making different taggs and let it look for the value via that, but so far we kept being told that working with strings like that is best avoided.

So I am looking for a way to only interact with the object I colided with and get the value out of a scripts that is from that unit or projectile only.

Beside that the even more important question how I best could have found a way for it myself. It is hard for me so far to find a way if i am not sure where to look.

ps. Sorry for the maybe unnecessary side information around my question

Hi Aqoun,

It’s great to see that you are challenging yourself. :slight_smile:

Could you please explain what these two lines are supposed to do in the first class?

enemyHealth = FindObjectOfType<EnemyHealth>();
Damage damage = FindObjectOfType<Damage>();

Be as precise as possible.

So I am looking for a way to only interact with the object I colided with and get the value out of a scripts that is from that unit or projectile only.

The OnParticleCollision method comes with a parameter. Have you already checked the description in the API? Maybe the solution for your problem is that parameter.

Beside that the even more important question how I best could have found a way for it myself. It is hard for me so far to find a way if i am not sure where to look.

If you answer my first question in this reply, you’ll probably learn something important about Unity that will help you with similar problems. :slight_smile:

He nina,

Thank you for your reply again ^^

Those look for the first active object in <> and lets me use its public information in this script.
I have to admit that I am not sure what “First active object” means precisely but I know that is the reason why it went weird with my previous try when i had multiple enemies.

I have looked over there but I think there is something I don’t really understand fundamental about it because I couldn’t make it work. I have tried some things with the other.GetComponent like:

    getDamage = other.GetComponent<Damage>();
    enemyHealth.ProcessDamage(getDamage.amountOfDamage);

but I got the error:

Assets\Enemy\HitProcess.cs(19,35): error CS0103: The name ‘getDamage’ does not exist in the current context

I still don’t really get the error

But re-writing it as

enemyHealth.ProcessDamage(other.GetComponent<Damage’>().amountOfDamage);

seems to work for now ? I will have to test later today or tomorrow and see if it will work with different values now.

I’ll keep you up-to-date

(for some reason it removes the damage in <> completely so just ignore the ’ in there for the actual code

Yes, that’s correct. We don’t know what Unity defines as “first” but if we have multiple objects, “first” basically translates to “random”. We usually don’t want a “random” object but a specific one. For this reason, FindObjectOfType when trying to deal damage to a specific object is usually wrong.

getDamage = other.GetComponent<Damage>();
enemyHealth.ProcessDamage(getDamage.amountOfDamage);

This looks fine. You fetch the Damage object from the game object that has been passed on to the OnParticleCollision method. However, the other game object must have a Damage object attached. Otherwise, getDamage will be null (= no object reference).

Where did you declare the variable getDamage? A declaration always needs a type. In your code snippet, I cannot see the type.

And where does enemyHealth come from?

That feels slightly reassuring knowing some things are just weird and we don’t know why.

That explains why it wasn’t working as i wanted, Because those 2 line were all that I wrote for it.

That one didn’t needed me to decleare it somewhere else so it just worked. Tested it as well and got my other towers is the game so it worked out so far ^^

Thank you for you thoughts and I will post a show for my progress with it

To increase the readability of your code, the local variable was a good idea. I assume you already know how to fix your code with the local variable. If not, here is the solution:

Damage damage = other.GetComponent<Damage>();
enemyHealth.ProcessDamage(damage.amountOfDamage);

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms