Trigger problem with bullet

Hey guys,

I have a small problem with my bullet script.
So let’s start with the script:

	void Start () {

		if (transform.parent != null) {
			fightingRange = transform.parent.gameObject.GetComponent<C1FightingRange> ();
			stats = transform.parent.gameObject.GetComponent<C1Stats> ();		
		}		
		
	}

void OnTriggerEnter2D(Collider2D coll)
	{
		if (coll.tag != "Player" && coll.tag != "Bullet") {
			isColliding = true;
			rigidbody2D.velocity = Vector2.zero;
			
			if (coll.tag == "Enemy") {
				
				totalDamage = (stats.baseDamage * fightingRange.attackDamage)/coll.transform.gameObject.GetComponent<E1Stats> ().baseDefense;
				
				if (totalDamage < 0) {
					totalDamage = 0;
				}
				coll.transform.gameObject.GetComponent<E1Stats> ().baseLife = coll.transform.gameObject.GetComponent<E1Stats> ().baseLife - totalDamage;
				
			}
		}
	}

My bullet is a child of the player.
I instanciate it like this:

bullet =  Instantiate(bulletPrefab, bulletSpawnPosistion,  Quaternion.identity) as GameObject;
	bullet.transform.parent = transform;

The script as it work’s fine. But the only problem is then i stand very closely to my enemy so that the bullet spawns in the collider of the enemy i get a “NullReferenceException: Object reference not set to an instance of an object” error in the the following line:

totalDamage = (stats.baseDamage * fightingRange.attackDamage)/coll.transform.gameObject.GetComponent<E1Stats> ().baseDefense;

To be exact the stats.baseDamage part.
And I could’t set the “Start” part into “Awake” because then I get a “NullReferenceException: Object reference not set to an instance of an object” error from the “transform.parent.gameObject.GetComponent ();” line.

Soooo what am I doing wrong?^^

Thank you very much for your time :slight_smile:

can you print:

coll
coll.transform
coll.transform.gameObject
coll.transform.gameObject.GetComponent
coll.transform.gameObject.GetComponent ().baseDefense
stats.baseDamage
fightingRange.attackDamage

please? see which one is null?

my guess is that since you spawned inside the trigger collider, you may have not entered it. in a way that there is no coll in there. im not sure about it but if you find out that coll is null you may wanna try to add some OnTriggerStay2D(Collider2D coll)
action to fix the situation