Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Strange Error

Discussion in '2D' started by tito231, Oct 20, 2022.

  1. tito231

    tito231

    Joined:
    Dec 25, 2021
    Posts:
    5
    Hello im working on a upgraded version of "Space Invaders", I'm new in programming and when im pressing space, the error is showing.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerShootManager : MonoBehaviour
    6. {//---------------Class---------------
    7.  
    8.     //---------------Variables---------------
    9.     [SerializeField] private GameObject[] bullet;
    10.     private bool busy = true;
    11.     private int bulletInstantiotionType;
    12.     //------------END-Variables--------------
    13.  
    14.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    15.  
    16.     //---------------Base-Functions---------------
    17.     private void Update()
    18.     {
    19.         bulletInstantiotionType = gameObject.GetComponent<PlayerVisualManager>().spaceShipType;
    20.         if(Input.GetKey(KeyCode.Space) && busy)
    21.         {
    22.             StartCoroutine(PlayerShoot0(bullet[bulletInstantiotionType]));
    23.         }
    24.     }
    25.     //------------END-Base-Functions--------------
    26.  
    27.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    28.  
    29.     //---------------Custom-Functions---------------
    30.     IEnumerator PlayerShoot0(GameObject i)
    31.     {
    32.         Instantiate(i, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 1.5f, 0f), Quaternion.identity);
    33.         busy = false;
    34.         yield return new WaitForSeconds(0.5f);
    35.         busy = true;
    36.     }
    37.     //------------END-Custom-Functions--------------
    38.  
    39.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    40.  
    41. }//-----------END-Class---------------
    This error:
    2022-10-20_23h43_11.png

    I was trying to resolve it but i can't. Could someone help me please?
     
  2. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    @tito231 what could be better than space invaders!

    This is a runtime exception so at least you got it to compile ;) The error is telling you that the array index at line 22 is out of bounds, so let's have a look at line 22. There is an array there called bullet and it is being indexed by a variable called bulletInstantionType. An array index is used to access one of the elements of the array. The element returned will correspond to the index. If you have ten elements in your array then you can only index it with the integers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). Any other integer will throw that exception you got. If you try to index it with a variable that is not an integer you will get a compiler error. So I am inferring that the variable called bulletInstantionType must be an integer even though its name indicates it is some kind of type. To debug this further add the following line between lines 21 and 22.

    Code (CSharp):
    1. Debug.Log($"index is {bulletInstantionType}");
    Run it again and you will see all the values of the index being used to index the bullet array before the exception happens. This might help you figure out why you have the wrong value.

    If any of the words you just read don't make sense then I suggest you spend some time learning C# with one of the many amazing tutorials that are out there. I think that may save time in the long run and increase chances of success for your Space Invaders upgrade project.
     
  3. tito231

    tito231

    Joined:
    Dec 25, 2021
    Posts:
    5
    @dynamicbutter thx for replay but.
    I done that and bulletInstantiotionType var is of type int and spaceShipType too 2022-10-21_16h24_01.png
    and the value is 0 so i don't know where the problem is.
     
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    637
    First thing in the morning, i see this didnt get resolved from yesterday. Honestly, from what i can see here, im not sure the error is here. Maybe a reference missing in the inspector or ... not sure. Anyways, if you really stuck , hit me up on discord mate, i will take a look with you. GL

    JD#1539
     
    tito231 likes this.
  5. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    So the bullet array is empty. How are you adding elements to the bullet array? One thing to check is that you have added your bullet elements before the first call of Update.
     
  6. tito231

    tito231

    Joined:
    Dec 25, 2021
    Posts:
    5
    The thing is that the bullet is spawning but this error too
     
  7. tito231

    tito231

    Joined:
    Dec 25, 2021
    Posts:
    5
    Homicide likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    Much like a null reference, there's very specific steps for indexing errors:

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Steps to success:
    - find which collection it is (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection
    - remember you might have more than one instance of this script in your scene/prefab
     
  9. tito231

    tito231

    Joined:
    Dec 25, 2021
    Posts:
    5
    @Homicide solved it but anyways thx for that

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerShootManager : MonoBehaviour
    6. {//---------------Class---------------
    7.  
    8.     //---------------Variables---------------
    9.     [SerializeField] private GameObject bullet;
    10.     private bool busy = true;
    11.     private int bulletInstantiotionType;
    12.  
    13.     //------------END-Variables--------------
    14.  
    15.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    16.  
    17.     //---------------Base-Functions---------------
    18.     private void Update()
    19.     {
    20.         if(Input.GetKey(KeyCode.Space) && busy)
    21.         {
    22.             StartCoroutine(PlayerShoot0());
    23.         }
    24.     }
    25.     //------------END-Base-Functions--------------
    26.  
    27.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    28.  
    29.     //---------------Custom-Functions---------------
    30.     IEnumerator PlayerShoot0()
    31.     {
    32.         if(!bullet) yield break;
    33.  
    34.         Instantiate(bullet, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 1.5f, 0f), Quaternion.identity);
    35.         busy = false;
    36.         yield return new WaitForSeconds(0.5f);
    37.         busy = true;
    38.     }
    39.     //------------END-Custom-Functions--------------
    40.  
    41.     //----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    42.  
    43. }//-----------END-Class---------------
    44.  
    Code (CSharp):
    1. if(!bullet) yield break;
    that solved the error
     
  10. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    637

    OFC, i still think something somewhere is affecting this script, because the serialized reference is fullfilled (seen it with my own eyes), and theres nothing here in this script that would result in it being null.

    Sometimes its easier to get hands on and eyes on, thats why i throw myself out there on discord. As a self learned self everything, i know all to well how frustrating it can be to have to 'go it alone' for lots of various reasons.

    Personally i think its a hacky work around, but given the available information, for now it works. I would still be very wary that something is amiss ofc. Perhaps someone with better eyes then this old guy can spot something i missed. It happens. Could be its also happening in something we havent seen here.

    I recommend picking up a mic for those kind of get together sessions.

    Anyways GL.