So I am at loss. I am trying to find and disable script on each enemy object in game. I have foreach loop at awake that finds gameobjects with tag "Enemy", and finds script attached to it "EnemyDamagePlayer".
Now, on Death() function I would like to disable "EnemyDamagePlayer" on every enemy it manages to find.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class HealthOverall : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public AudioClip deathClip;
AudioSource playerAudio;
RagdollHelper ragdollHelper;
public bool isDead;
bool damaged;
public GameObject player;
Rigidbody rb;
public EnemyDamagePlayer[] damageScript;
// public AttackScript[] deadScript;
void Awake ()
{
playerAudio = GetComponent ();
player = GameObject.FindGameObjectWithTag ("Player3rdPerson");
ragdollHelper = player.GetComponent();
rb = player.GetComponent ();
//find scripts attached to every object with tag "Enemy"
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
damageScript = new EnemyDamagePlayer[enemies.Length];
for ( int i = 0; i < enemies.Length; ++i )
damageScript[i] = enemies[i].GetComponent();
Debug.Log ("Found you, dirty bastards!");
currentHealth = startingHealth;
}
void Update ()
{
}
public void TakeDamage (int amount)
{
currentHealth -= amount;
if(currentHealth <= 0 && !isDead)
{
// ... it should die.
Death ();
}
}
//Death Function
public void Death ()
{
isDead = true;
//here player is ragdolled
ragdollHelper.ragdolled=true;
//play death sound clip
playerAudio.clip = deathClip;
playerAudio.Play ();
//Make ThirdPersonController's Rigidbody Kinematic so ragdoll doesn't jerk around and disable controls
rb.isKinematic = true;
player.gameObject.GetComponent().enabled = false;
player.gameObject.GetComponent().enabled = false;
}
}
I am a bit bad with foreach functions and frankly I can't get it right. I'd love it someone showed me how to do it because I am tired of trying to find solutions online for couple of hours that don't work.
↧