I am having an issue with the following code, here is a general rundown. This script is attached to my enemy characters in my seen all Tagged with Enemy and all on the layer Enemy, also the enemy objects in the game are all stored under an empty gameobject named enemy. I currently only have two enemies in the world a capsule and a box each with this scrip attached and on the layer and tagged as above. I have tried multiple ways to get both objects to act equally, but what tends to happen is due to my code I have to place a gameObject into the variable field under the shoot script below. When I do that the only gameObject being referenced is the one dropped there, similarly when I attach it to the empty gameObject to store the enemies in the heirechy, it will let me shoot either one, but once one dies it destroys them both due to the Destroy(this.gameObject) in the enemyAttacked script. My goal is to have the scripts differentiate between the enemies who will all be tagged with Enemy and on the layer Enemy. Thanks for your help.
Anyway here is the code that is on the enemy:
using UnityEngine;
using System.Collections;
public class enemyAttacked : MonoBehaviour
{
//set up enemies health, and initialize the GUIText
int enemyHealth = 100;
public GUIText damageGaugeText;
public GUIText enemyHealthText;
void update(){}
public void attack()
{
//Make the GUIText visible to show player damage done, and enemy health
enemyHealthText.enabled = true;
damageGaugeText.enabled = true;
//calculate damage based off characters attack powers
int randomDamage = Random.Range(playerStats.minAttack, playerStats.maxAttack) * playerStats.attackPower;
//damage the current enemy
this.enemyHealth -= randomDamage;
//Used for debug to ensure damage is being done
Debug.Log ("You are attacking me");
//if the enemies health is below 0, set health value to 0, give the player 50 XP,
//destroy the current gameObject being attacked, and disable the GUIText for player
//damage gauge and enemy health display
if(this.enemyHealth < 0)
{
enemyHealth = 0;
playerStats.currentXP += 50;
Destroy(this.gameObject);
enemyHealthText.enabled = false;
damageGaugeText.enabled = false;
}
else
{
//Display the enemies health and damage done
enemyHealthText.text = "Enemy Health " + this.enemyHealth;
damageGaugeText.text = "Damage " + randomDamage;
}
}
}
Here is the shooting script I have constructed to detect when you are firing on an enemy, this is attached to my player:
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour
{
int count = 0;
public enemyAttacked ea; //created to access the attack variable, assigned to the empty gameobject i store my enemies under
// Update is called once per frame
void Update ()
{
//GameObject[] bulletTexture; // creates an array to use random textures of bullet holes
Vector3 fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
RaycastHit hit;
Debug.DrawRay(transform.position, fwd * 30, Color.red); //drays our raycast and gives it a green color and a length of 10 meters
if(Input.GetButtonDown ("Fire1") Physics.Raycast(transform.position, fwd, out hit, 30))
{
//when we left click and our raycast hits something
count++;
Debug.Log("You are shooting!! Shot number: " + count);
//
if(hit.collider.gameObject.name == "Enemy")
{
Debug.Log("Calling attack on enemy.");
ea.attack();
}
}
}
}