Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Null reference exception when instantiating objects using a for loop.

Discussion in 'Getting Started' started by DegaOne, Feb 4, 2017.

  1. DegaOne

    DegaOne

    Joined:
    Feb 4, 2017
    Posts:
    5
    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:
    Code (csharp):
    1.  
    2. void DestroyFireballSpecial(){
    3.         for (int i = 0; i < fireballAngles.Length; i++) {
    4.             //Instantiate the fireballs accordingly on right position with right rotation
    5.             var fireballObject = Instantiate (fireballs,transform.position,Quaternion.AngleAxis(fireballAngles[i], Vector3.forward)) as GameObject;
    6.             //Set their speed based on angle
    7.             fireballObject.GetComponent<FireballController>().ShootFireball(fireballAngles[i]);
    8.         }
    9.         Destroy(gameObject,0.0f);
    10.     }
    11.  

    And the public function on the fireball:

    Code (csharp):
    1.  
    2.     //Shoot fireball
    3.     public void ShootFireball(float angle){
    4.         rb.velocity = new Vector2(Mathf.Cos(angle*Mathf.Deg2Rad),Mathf.Sin(angle*Mathf.Deg2Rad))*speed;
    5.     }
    6.  
    7.  
    Edit: In case you guys want to see the collision function

    Code (csharp):
    1.  
    2.     //Damage enemy
    3.     void OnTriggerEnter2D(Collider2D other){
    4.         if (other.CompareTag("Enemy")){
    5.             //Deal damage
    6.             other.GetComponent<EnemyHealth>().TakeDamage(damage);
    7.             //Destroy fireball
    8.             DestroyFireballSpecial();
    9.         }
    10.     }
    11.  
    Thanks in advance :)
     
    Last edited: Feb 4, 2017
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. DegaOne

    DegaOne

    Joined:
    Feb 4, 2017
    Posts:
    5
    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.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    probably the "rb" variable is not yet set then, can you show the code where you assign it?
     
    JoeStrout likes this.
  5. DegaOne

    DegaOne

    Joined:
    Feb 4, 2017
    Posts:
    5
    Yea! sure, its on the start() function of each fireball , thats what i use for initializing references
    Code (csharp):
    1.  
    2. //Initialize variables
    3.     //Publics
    4.     public float speed;
    5.     //Privates
    6.     private int damage;
    7.     private Rigidbody2D rb;
    8.     PlayerController playerController;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         //Set damage from player object
    13.         playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
    14.         damage = playerController.attributes.damage;
    15.         rb = GetComponent<Rigidbody2D> ();
    16.     }
    17.     //Shoot fireball
    18.     public void ShootFireball(float angle){
    19.         rb.velocity = new Vector2(Mathf.Cos(angle*Mathf.Deg2Rad),Mathf.Sin(angle*Mathf.Deg2Rad))*speed;
    20.     }
    21.  
     
    Last edited: Feb 4, 2017
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    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..
     
    Hozgen90 likes this.
  7. DegaOne

    DegaOne

    Joined:
    Feb 4, 2017
    Posts:
    5
    Yes , you are right . Changing Start to Awake fixes the problem.
    Thanks!