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);
}
}
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.
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
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.
– Voridian.GetComponent< Enemy > () <--
– ahaykalthank you both VERY much i have been stuck on this for ages.
– Voridian