Hello everybody , this is my first post so if I’m not on the right place I apologize.
My project is a shmup and I’m trying to instantiate some ‘fireball’ game objects giving each one of them a different angle. This happens when the prefab for a bigger fireball gets destroyed. The error I keep getting is
NullReferenceException: Object reference not set to an instance of an object
FireballController.ShootFireball (Single angle) (at Assets/Assets/Scripts/Player/FireballController.cs:22)
FireballSpecialController.DestroyFireballSpecial () (at Assets/Assets/Scripts/Player/FireballSpecialController.cs:43)
FireballSpecialController.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Assets/Scripts/Player/FireballSpecialController.cs:34)
Edit: For some reason I can’t write the square brackets on fireballAngles, but in my code its got those with ‘i’ as the index.
This is my code:
void DestroyFireballSpecial(){
for (int i = 0; i < fireballAngles.Length; i++) {
//Instantiate the fireballs accordingly on right position with right rotation
var fireballObject = Instantiate (fireballs,transform.position,Quaternion.AngleAxis(fireballAngles[i], Vector3.forward)) as GameObject;
//Set their speed based on angle
fireballObject.GetComponent<FireballController>().ShootFireball(fireballAngles[i]);
}
Destroy(gameObject,0.0f);
}
And the public function on the fireball:
*_</em></em> <em><em><em>* //Shoot fireball public void ShootFireball(float angle){ rb.velocity = new Vector2(Mathf.Cos(angle*Mathf.Deg2Rad),Mathf.Sin(angle*Mathf.Deg2Rad))*speed; }*</em></em></em> <em><em>_*
Edit: In case you guys want to see the collision function
*_</em></em> <em><em><em>* //Damage enemy void OnTriggerEnter2D(Collider2D other){ if (other.CompareTag("Enemy")){ //Deal damage other.GetComponent<EnemyHealth>().TakeDamage(damage); //Destroy fireball DestroyFireballSpecial(); } }*</em></em></em> <em><em>_*
Thanks in advance ![]()