Parent collider sometime counting as child's collider
Hi guys, I'm pretty new to coding so please excuse me if I miss anything obvious. I have a game object called 'FireSpawner' which is a child of my 'Player' game object. Attached to FireSpawner I have a script called 'UpgradesScript'. 'FireSpawner' is supposed to instantiate a prefab 'fire' every time its collider isn't in contact with a 'fire', leaving a trail of 'fire's behind the player as it moves.
The spawner works fine to begin with but if an enemy hits Player's collider while FireSpawner and a fire have overlapping colliders; FireSpawners counter value goes up making the fires stop instantiating. All I can think of is setting the counter to 0 when the enemy hits the player, but if anyone knows of a way to stop the counter from going out in the first place I would really appreciate your time.
Edit The count doesn't go out when an enemy collides with the player unless the player is trying to move into the enemy. if the enemy hits the player while the player isnt moving but is still over a fire the counter still works. I'm not sure why
using UnityEngine;
using System.Collections;
public class UpgradesScript : MonoBehaviour {
public GameObject fire;
public static int a=0;
private Transform trans;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
GameObject Player = GameObject.FindGameObjectWithTag("Player");
PlayerScript playerScript = Player.GetComponent<PlayerScript>();
if(a==0)
{
Instantiate(fire, playerScript.trans.localPosition, Quaternion.identity);
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if((coll.gameObject.tag == "Fire") && (coll.gameObject.tag != "Enemy"))
{
a++;
}
}
void OnTriggerExit2D(Collider2D coll)
{
if((coll.gameObject.tag == "Fire") || (coll.gameObject.tag == "Enemy"))
{
a--;
}
}
}