Dealing damage problem
I have 3 scripts that is supposed to deal damage to my enemy (for melee combat). And also both the player and the enemy has the "Stats" script attached to them because i want this to be multiplayer at a later stage. This is the first script:
using UnityEngine;
using System.Collections;
public class CharacterInputdenandra : MonoBehaviour
{
void FixedUpdate () {
ControllPlayer()
}
void ControllPlayer () {
if (Input.GetButtonDown ("Fire1")) {
GameObject go = Instantiate(Resources.Load("GS_Hitbox"), transform.position+(transform.forward*2)+(transform.up*2)+(transform.right*-1.5f) , transform.rotation) as GameObject;
go.transform.parent = transform;
Gs_HITDETECT = go;
Invoke ("Amove",0.4f);
}
}
void Amove () {
DestroyObject (Gs_HITDETECT);
}
}
This is used to create the prefab which is a trigger that detects if any enemy is inside. The prefab has this script attached to it:
using UnityEngine;
using System.Collections;
public class HitDetection : MonoBehaviour {
public GameObject enemy;
public int Weapondamage;
Stats enemyhealth;
void Awake () {
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Weapondamage = transform.root.gameObject.GetComponent<Stats> ().damage;
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Enemy") {
enemy = other.gameObject;
enemyhealth = enemy.GetComponent<Stats>();
enemyhealth.TakeDamage (Weapondamage);
print ("hit");
}
}
}
Until here evrything works fine. if the enemy is hit i get the print right. The problem is when i acctually want to deal the damage in the Stats script attached to the enemy.
using UnityEngine;
using System.Collections;
public class Stats : MonoBehaviour {
public int maxhealth = 100;
public int curhealth = 100;
public int damage = 12;
public bool hascollide = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (curhealth <= 0f) {
Destroy(gameObject);
}
}
public void TakeDamage (int amount) {
curhealth -= amount;
}
}
The wierdest thing is that if i run the game as it is i cant deal any damage to the enemy, but if i go back to the first script and remove the part where i destroy the trigger, i can move around so just one "GS_Hitbox" can hit the enemy more than once, the enemy takes damage.
So to sum this up the problem is that my trigger does not deal damage when instantiated. Can someone please help me with this issue?
I don't see any obvious problems in your code on my mobile phone. I did notice you are using a lot of public variables though. Any chance you've changed them over time...? If so, you might want to check the editor to see if the editor is still enforcing the old values (as public variables can be set from the editor).
well the public variables is not the problem since it works when its not instantiated, i just need to know why it doesn't work when the trigger is instantiated :S