- Home /
Enemy wont damage player
Hey, im kinda a beginner so i dont understand complex stuff. im trying to make a game where the enemy will damage the player when it walks inside of my box collider The enemy has a box collider set to trigger This is my scripts:
Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public bool inRange;
public float damage;
void Start ()
{
}
void Update ()
{
if(inRange == true)
{
//attack
PlayerStats player = GetComponent<PlayerStats>();
player.P_TakeDamage(damage);
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
inRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
inRange = false;
}
}
}
Player Script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerStats : MonoBehaviour {
public float currHealth;
public float maxHealth;
void Start ()
{
}
public void P_TakeDamage(float ammount)
{
currHealth -= ammount;
}
void Update ()
{
if (currHealth > maxHealth)
{
currHealth = maxHealth;
}
if (currHealth <= 0)
{
//Die
Die();
}
}
public void Die()
{
Destroy(gameObject);
}
}
Answer by Santosh_Nair · Oct 13, 2018 at 04:54 PM
Did you try debugging the code and watch values of inRange and currHealth ?
@Santosh_Nair hmmm.. i dont really know what that means? as i said im a noob sorry, is it this? https://unity3d.com/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio
yes, by debugging I meant the same. Is your PlayerStat script and EnemyAttack script attached to the same gameobject? if yes, you need to put it as this.gameobject.Getcomponent() if no, then you need to put it as Gameobject.FindWithTag(Your Player Gameobject Tag).GetComponent()
Currently, your player variable is returning null value. Could be because it is not able to find PlayerStats.
@Santosh_Nair if i understand it right u want me to put Gameobject.FindWithTag("Player").GetComponent()
in the if(inRange == true)
?? but when i do it it says error The name Gameobject does not exist in the current context
and it looks like it can find PlayerStats because when i type player. under the
PlayerStats player = GetComponent<PlayerStats>();
it comes up all variables from the playerstats script
EDIT: just found out it said this when inRange == True: NullReferenceException: Object reference not set to an instance of an object EnemyAttack.Update () (at Assets/Scripts/EnemyAttack.cs:24)
EDIT2: i changed Gameobject.FindWithTag("Player").GetComponent() to GameObject.FindWithTag("Player").GetComponent<PlayerStats>();
it said that i should do that, and now that error is gone but i still dont take damage
Answer by ElementalVenom · Oct 13, 2018 at 04:05 PM
Do they both have a rigidbody component attached? If not make sure to add one to each otherwise IIRC the OnTriggerEnter and OnTriggerExit will never be called. Otherwise click on the enemy in the editors hierarchy and check wither the "inRange" variable is set to true when the player gets near.
@ElementalVenom It says its true when i get close, but it wont damage the player. ive also tried to change
player.P_TakeDamage(damage);
to
player.currHealth -= damage;
but that doesnt work wither.
Your answer
Follow this Question
Related Questions
Enemy wont damage player 0 Answers
Enemy won't damage player C# 1 Answer
Run coroutine only when player stays inside collider? 0 Answers
Multiple Cars not working 1 Answer
How to make player stop a certain distance away from enemy? 1 Answer