- Home /
Why does the wrong enemy die with this raycast? C#
Okay well I don't really know how to phrase this but basically I have a Raycast script that says that if it hits something tagged "Enemy" then to take -25 of the health int. only problem is that no matter which enemy I shoot only the one I declared as a GameObject will destroy and I cant figure out how to use a prefab as a GameObject. On top of that while using a debug I found out that both the enemy objects health decreases when only one is shot... I hope somebody can see the issue.
Raycast Script:
using UnityEngine;
using System.Collections;
public class WeaponRaycast : MonoBehaviour {
public GameObject Player;
float isRay = 0;
public GameObject bulletDecal;
public GameObject enemyObject;
private Enemy enemy;
void Awake () {
enemy = enemyObject.GetComponent<Enemy>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log(enemy.health);
RaycastHit hit;
if(Input.GetButtonDown("Fire1")){
if(Physics.Raycast(Player.transform.position, Player.transform.forward, out hit, 20f)){
if(PistolController.clipSize > 0 && PistolController.canFire == true){
if(hit.collider.gameObject.CompareTag("Enemy")){
isRay = 1;
enemy.health = enemy.health - 25;
}
else{
Instantiate(bulletDecal, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
isRay = 0;
}
}
}
}
Debug.DrawRay(transform.position, transform.forward, Color.green);
}
}
Enemy script:
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public int health = 100;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(health <= 0){
Destroy(gameObject);
}
}
}
Thanks in advance if you can work it out.
Answer by Jamora · Feb 24, 2014 at 06:38 AM
It seems you get a reference to only one enemy; the one that is on the same GameObject as the one you define in the inspector. (Line 14)
You need to change line 30 to actually use the Enemy script in the GameObject that was hit. Something like:
hit.collider.gameObject.GetComponent< Enemy >.health -= 25;
hey there, thanks for the reply but it comes up with a "expression denotes a 'method group', where a 'variable', 'value' or 'type' was expected." when i alter line 30 to what you suggested.
thank you both VERY much i have been stuck on this for ages.
Answer by black1ops22 · Mar 17, 2015 at 07:21 AM
Also, just for fun (im giving it away for freeeeee) i created a universal gun script with firerate, FX and more!
TEST IT OUT:
using UnityEngine; using System.Collections; //Bulletspawnpoint MUST be named Bulletspawnpoint public class Tryintoscriptpewpews : MonoBehaviour {
public Rigidbody Bullet; // Bullet, Bulletspawnpoint, Automatic, isShooting,Bulletsound, Magsize, Ammo, firerate
public GameObject Barrelflash;
public GameObject Bulletholesmoke;
public GameObject Bulletspawnpoint;
public GameObject Bullethole;
public GameObject Blood;
public bool Automatic;
public float Range = 100f;
public float Firerate = 8f;
public float Magsize = 30f;
public float Amountofclips = 3f;
public float Damage = 15f;
private float Counter = 0f;
private bool isShooting;
public AudioClip Bulletsound;
RaycastHit hit;
// These are the customizable values. // Create a spawnpoint and place it infront of ze gun
void Update(){
Counter += Time.deltaTime;
if(Automatic == true) {
if(Input.GetKey (KeyCode.Mouse0) && Firerate > Counter){
isShooting = true;
if(isShooting == true){Fire();}
}
Counter = 0f;
}
else {
if(Input.GetKeyDown (KeyCode.Mouse0)){
isShooting = true;
if(isShooting == true){Fire();}
}
}
}
void Fire (){
Instantiate (Bullet, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
Bullet.velocity = Vector3.forward * 8;
AudioSource.PlayClipAtPoint (Bulletsound, transform.position);
if (Physics.Raycast (GameObject.Find ("Bulletspawnpoint").transform.position, transform.forward, Range)) {
if(hit.collider.gameObject.tag == "Player"){
Instantiate(Blood, hit.point, hit.transform.rotation);
hit.collider.gameObject.SendMessage("ApplyDamage", Damage);
}
}
}
}
Its not done tho
Your answer
Follow this Question
Related Questions
How can i modify a float in another script in C#? 1 Answer
AI Shooting for Aircraft 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Limited ammo from Raycast? 1 Answer