Search Unity

Firing Script for shooter throws IndexOutOfRangeExceptions and MissingReferenceExceptions

Discussion in 'Scripting' started by AncientShotgun, Dec 27, 2016.

  1. AncientShotgun

    AncientShotgun

    Joined:
    Dec 27, 2016
    Posts:
    5
    Hi guys, gals and anybody else who might be here,

    I have this code, that is attached as a script to a child object of an enemy in my top-down vertically-scrolling shooter:
    Code (CSharp):
    1. public GameObject[] firePatterns;
    2.     public int fireDelayInFrames;
    3.     public bool isFinishedFiring;
    4.  
    5.     private int frameControl = 0;
    6.     private int fireControl = 0;
    7.  
    8.     private void Update()
    9.     {
    10.         if (GetComponentInParent<EnemyShooter>().readyToFire)
    11.         {
    12.             frameControl++;
    13.             if ((frameControl % fireDelayInFrames) == 0)
    14.             {
    15.                 if (fireControl > firePatterns.Length)
    16.                 {
    17.  
    18.                     isFinishedFiring = true;
    19.                 }
    20.                 else
    21.                 {
    22.                    
    23.                     Instantiate(firePatterns[fireControl], transform.position, transform.rotation);
    24.                     Debug.Log("Emitter Script: Should fire here.");
    25.                     fireControl++;
    26.                 }
    27.                 frameControl = 0;
    28.             }
    29.  
    30.  
    31.         }
    32.     }
    It works fine up until the last object in the array is instantiated, at which point it throws an IndexOutOfRangeException, of course pointing to the line with the Instantiate() method on it. I know that this Exception is only called when the integer used to refer to an array's index lies beyond the scope of that index, but my if checks should stop this from happening.

    Additionally, and also possibly more important to find out the cause of, the code snippet above also randomly causes MissingReferenceExceptions,once again on the line with the Instantiate() method, to be thrown when the script is run.

    I would also like to improve upon this script, perhaps by constructing it as a coroutine and calling it from the instead of calling it once every frame in the Update() method. However, i am somewhat lackluster in skill when it comes to coroutines.

    Could one of you fine people provide me with some advice/help on what is happening with my code snippet, and also provide insight as to how I could restructure this as a coroutine instead?
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    384
    if (fireControl >= firePatterns.Length) { ...
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You didn't read the developer network rules
     
  4. AncientShotgun

    AncientShotgun

    Joined:
    Dec 27, 2016
    Posts:
    5
    Apologies. Since I am assuming that this subforum was the wrong place to start this thread, which one would have been the right one to start the thread in?

    I also see that the title of my question was, in fact, very vaguely worded, so if that was it, then I once again apologise for any infractions made.
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    No, the problem is your title, it should be about the thread itself, not please help or something
     
  6. AncientShotgun

    AncientShotgun

    Joined:
    Dec 27, 2016
    Posts:
    5
    Thank you for the rapid reply. Unfortunately, while this has solved the IndexOutOfRangeExceptions, the MissingReferenceExceptions are still being thrown, this time always on the second-to-last objects to be instantiated. Is this a solvable issue, or will I have to restructure my code entirely?
     
  7. AncientShotgun

    AncientShotgun

    Joined:
    Dec 27, 2016
    Posts:
    5
    Is there a way to change the title of a thread? Or is it stuck like this permamently?
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    There's a thread tools button right under the title
     
  9. AncientShotgun

    AncientShotgun

    Joined:
    Dec 27, 2016
    Posts:
    5
    Siginificantly more descriptive title added, thank you for helping me out.
     
unityunity