# Laser Damage

**URL:** https://discussions.unity.com/t/laser-damage/657015
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 2, 2017, 7:50am UTC](https://discussions.unity.com/t/laser-damage/657015 "2017-03-02T07:50:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Jrusso17](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jrusso17](https://discussions.unity.com/u/Jrusso17)
#### Post date: [March 2, 2017, 7:50am UTC](https://discussions.unity.com/t/laser-damage/657015/1 "2017-03-02T07:50:23Z")

</div>

Hello i need some help with a laser script  
i have the laser working but i want to add a damage source so it applies constant dmg to the target

using UnityEngine;  
using System.Collections;

public class LaserScript : MonoBehaviour  
{  
LineRenderer line;  
public float m\_MaxDamage = 1f;  
void Start()  
{  
line = gameObject.GetComponent();  
line.enabled = false;  
}  
void Update()  
{  
if (Input.GetButtonDown(“Fire1”))  
{  
StopCoroutine(“FireLaser”);  
StartCoroutine(“FireLaser”);  
}  
}  
IEnumerator FireLaser()  
{  
line.enabled = true;

while (Input.GetButton(“Fire1”))  
{  
Ray ray = new Ray(transform.position, transform.forward);  
RaycastHit hit;

line.SetPosition(0, ray.origin);

if (Physics.Raycast(ray, out hit, 100))  
{  
line.SetPosition(1, hit.point);  
if (hit.rigidbody)  
{  
hit.rigidbody.AddForceAtPosition(transform.forward \* 10, hit.point);

}  
}  
else  
line.SetPosition(1, ray.GetPoint(100));

yield return null;  
}

line.enabled = false;  
}  
}  
this is the script it is c#

---

<div class="post-metadata">

### Author: ![Rogalog](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@Rogalog](https://discussions.unity.com/u/Rogalog)
#### Post date: [March 2, 2017, 1:17pm UTC](https://discussions.unity.com/t/laser-damage/657015/2 "2017-03-02T13:17:59Z")

</div>

Hi Jrusso17,

Firstly, please use code tags as they increase the readability of your code on the forums: [Using code tags properly - Unity Engine - Unity Discussions](https://forum.unity3d.com/threads/using-code-tags-properly.143875/)

As for your damaging, there are two ways you could go about this:

1. Although this method is generally frowned upon, you could call `hit.transform.gameObject.SendMessage("Damage")` within your while loop. This will go through all your scripts that you have attached onto your target and attempt to execute the “Damage” function, assuming you’ve implemented it. Generally not recommended as it can be quite slow.

2. You could ensure that any object you want to be damageable has a common script (you could use inheritance or interfaces here) that you can call a default “Damage” function on for example. It would go something like this.

```csharp
public interface IDamageable {

    void Damage(float damage);

}

```

Have a script on your targets that you want to be able to be damaged that uses this interface and implement the Damage function appropriately. An example would be the following ( **this is the script that goes on your enemy** ):

```auto
public class Enemy : MonoBehaviour, IDamageable {

    public float health = 100;

    public void Damage(float damage)
    {
        health -= damage;
    }

}

```

Then in your while loop you can do something similar to:

```csharp
while (Input.GetButton("Fire1"))
{
    Ray ray = new Ray(transform.position, transform.forward);
    RaycastHit hit;

    line.SetPosition(0, ray.origin);

    if (Physics.Raycast(ray, out hit, 100))
    {
        line.SetPosition(1, hit.point);
        if (hit.rigidbody)
        {
                hit.rigidbody.AddForceAtPosition(transform.forward * 10, hit.point);
        }

        IDamageable target = hit.transform.GetComponent<IDamageable>();
        if (target != null)
            target.Damage(10); // Replace this value with whatever damage you want to be dealt each frame.

    }
    else
        line.SetPosition(1, ray.GetPoint(100));

    yield return null;
}

```
