- Home /
Ontrigger not working with npc
Ok so i have this script where if the npc walk in, the npc would receive damage, but nononono it just doesn't work, it's taking me quite awhile now to solve and been searching but i just can't get it to work. This script damages both player and enemy, but i had to change it to something that it would force the script to find the npchealth script and damage it but its still not working.
Here is the script : public PlayerHealth scriptplayerhealth; public NPCHEALTH scriptnpchealth; public float hurt = 10; public float hitLast = 0; public float hitDelay = 1;
// Start is called before the first frame update
void Start()
{
}
public void Update()
{
}
public void OnTriggerStay(Collider other)
{
if (Time.time - hitLast < hitDelay)
return;
if (other.gameObject.tag == "NPC")
{
other.gameObject.GetComponent<NPCHEALTH>().TakeDamage(hurt);
}
if (scriptnpchealth != null)
return;
else
if (other.gameObject.tag == "Player")
{
scriptplayerhealth = other.gameObject.GetComponent<PlayerHealth>();
scriptplayerhealth.PlayerTakeDamage(hurt);
}
hitLast = Time.time;
}
and here is the npc health script:
public float enemyHealth = 25f;
public GameObject DeadPrefab;
public void Update()
{
}
public void TakeDamage(float amnt)
{
enemyHealth -= amnt;
if (enemyHealth <= 0)
{
Instantiate(DeadPrefab, transform.position, transform.rotation);
{
Destroy(this.gameObject);
}
}
}
public void NPCTakeHealth(float amnt)
{
enemyHealth += amnt;
}
Your answer
Follow this Question
Related Questions
Another function for activating a different script 0 Answers
Script is only working on original object 2 Answers
Enemy evade sight of player 1 Answer
Enemy doesn't avoid static objects 2 Answers