How to iniate the damage on contact

The damage is automatically dealt to the boss and enemies with the tag “Enemy”. For example if i hit “z” 10 times the boss dies regardless if the shoots hit the boss or not. I need it to only do the damage when the shoots collide with boss.

using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour {

public GameObject laser;
public GameObject multi;
public GameObject multiup;
public GameObject multidown;
public GameObject missile;
public GameObject melee;
public GameObject[ ] physicsGibs;

void Update ()
{

if(Input.GetKeyDown(KeyCode.Z))
{

Instantiate(laser,transform.position,transform.rotation);

GameObject Enemy = GameObject.FindWithTag(“Enemy”);
BossHealth a = Enemy.GetComponent();
a.Health -= 1f;

}

if(Input.GetKeyDown(KeyCode.X))
{
Instantiate(multi,transform.position,transform.rotation);
Instantiate(multiup,transform.position,transform.rotation);
Instantiate(multidown,transform.position,transform.rotation);

GameObject Enemy = GameObject.FindWithTag(“Enemy”);
BossHealth a = Enemy.GetComponent();
a.Health -= 1f;

}

if(Input.GetKeyDown(KeyCode.C))
{
Instantiate(missile,transform.position,transform.rotation);

GameObject Enemy = GameObject.FindWithTag(“Enemy”);
BossHealth a = Enemy.GetComponent();
a.Health -= 1f;

}

if(Input.GetKeyDown(KeyCode.LeftControl))
{
Instantiate(melee,transform.position,transform.rotation);

GameObject Enemy = GameObject.FindWithTag(“Enemy”);
BossHealth a = Enemy.GetComponent();
a.Health -= 1f;

}
}

}

Dude, if you have trouble understanding some specific part of code tell us which one. However if you expect someone to do the job for you you should post this in “Commercial: Job Offering” subforum

Right okay, from what i gathered and the way i would do it. Is to add a collider to the boss and other enemies, so that unless your raycasts or however you plan on attacking the box hit’s the collider then the health will no be affected. Then after the collider as been it, make sure you script it to send a message to the health scripts. It is covered on the Unity Documentation. There is also ways in melee for example, where you can also set it to only “deal damage” when you are in a certain radius. Have a look on the documentation, if you still stuck, then reply here and i’ll help point you towards. But as Pirs01 said, don’t expect anyone to do it for you, because well, you won’t learn much compared to being pointed towards the right direction and finding it yourself.

I didn’t ask for anyone to do it for me, just wanted to be pointed in right direction. Second, if u read what said i just asking fora tip one a for something to get teh part with

GameObject Enemy = GameObject.FindWithTag(“Enemy”);
BossHealth a = Enemy.GetComponent();
a.Health -= 1f;

to only execute when the shoots collide with boss. If that is too much for u than don’t bother replying at all. I already did 95% of the work. KNow the different between someone asking for someone to do all the work and a small nudge.

OK, I got a solution for my coding. FYI, i did 95% of the work, was just asking for a tip on how one would code the script to only execute when it hit target and not when i ht key. IF expecting people to read my code and push me towards a possible solution counts than that like me saying u stole other people coding, so calm down. Lucky, i meet up with an actual programmer. He gave me a “tip”, which all i was asking for. He mentioned that I Add another script that calls the damage part using OnTriggerEnter with a prefab. If someone needs me to point out which part of the code does what than ask in a nice manner without the attitude. Drop the ego. My friend helped me without doing any work just fine. Once again, i got my answer so i won’t be needed anymore help, but than again i didn’t get any help.

You posted some unformatted code without any explanation and said what you want your game to work like. No one is going to clean up that code figure out what it does because you didn’t bother to explain and modify it to satisfy your wishes. Next time use “CODE” tag when posting code, explain what it is and what problem or question you have specifically.

For collisions check this out:

Basically you want your Boss class to have a collision listener:

void OnTriggerEnter(Collider collision)
{
}

In it Check what is it that collided with boss and when it’s a missile adjust his health.