Null reference exception when instantiating objects using a for loop.

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 :slight_smile:

Hi,

Which one is the actual error line of code?
Have you checked if the fireballs prefab has *FireballController-*script attached?

ps. here is info how to insert code into forum post,

Its the line on the ShootFireball function that sets the rigidbody’s velocity.
Thanks for the info, it should look alright now . And for the question , yes , each fireball has a FireballController script on it.

probably the “rb” variable is not yet set then, can you show the code where you assign it?

1 Like

Yea! sure, its on the start() function of each fireball , thats what i use for initializing references

//Initialize variables
    //Publics
    public float speed;
    //Privates
    private int damage;
    private Rigidbody2D rb;
    PlayerController playerController;

    // Use this for initialization
    void Start () {
        //Set damage from player object
        playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
        damage = playerController.attributes.damage;
        rb = GetComponent<Rigidbody2D> ();
    }
    //Shoot fireball
    public void ShootFireball(float angle){
        rb.velocity = new Vector2(Mathf.Cos(angle*Mathf.Deg2Rad),Mathf.Sin(angle*Mathf.Deg2Rad))*speed;
    }

Test with Awake() instead of Start() ?

but since you are instantiating it directly (not using pooling?),
in this case you could put that GetComponent inside ShootFireball(), if that fixes it…

1 Like

Yes , you are right . Changing Start to Awake fixes the problem.
Thanks!