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

Question My code sometimes spawns prefabs, sometimes doesn't

Discussion in 'Prefabs' started by CodeMateo, Jul 9, 2022.

  1. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Hello everyone, problem is as title is. I'm using the prefabs as a detecting surface to tell if the country nearby is enemy or ally. Some spawn, some don't. And only specific counties detect and get placed in AccountGame, while others don't. I'm not sure what I am doing wrong here, I have two main errors that relate to this. They are ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <e38a6d3ee47c43eb9b2e49c63fc0aa48>:0)
    Control+<SpawnBorder>d__19.MoveNext () (at Assets/Code/Control.cs:91)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    Control:Start() (at Assets/Code/Control.cs:32) and
    NullReferenceException: Object reference not set to an instance of an object
    Border.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Code/Border.cs:13). Typically I destroy gameObject but I have it // right now to see where the prefabs are being placed, so I'm not sure where this error is from especially since it pops up only twice. Here is my Border code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Border : MonoBehaviour
    6. {
    7.     public float sonController;
    8.  
    9.     public float borderNumber;
    10.  
    11.     void OnTriggerEnter2D(Collider2D other)
    12.     {
    13.         if (other.gameObject.GetComponent<Control>().controller == sonController)
    14.         {
    15.             Debug.Log("ally" + other.gameObject);
    16.             if (borderNumber == 0f)
    17.             {
    18.                 if (gameObject.GetComponentInParent<Control>().accounted0 == false)
    19.                 {
    20.                     if (sonController == 1f)
    21.                     {
    22.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies -= 1f;
    23.                     }
    24.                     gameObject.GetComponentInParent<Control>().accounted0 = true;
    25.                 }
    26.                 if (gameObject.GetComponentInParent<Control>().accounted0 == true)
    27.                 {
    28.                     if (gameObject.GetComponentInParent<Control>().accountGame0 != other.gameObject)
    29.                     {
    30.                         gameObject.GetComponentInParent<Control>().accounted0 = false;
    31.                     }
    32.                 }
    33.             }
    34.             if (borderNumber == 1f)
    35.             {
    36.                 if (gameObject.GetComponentInParent<Control>().accounted1 == false)
    37.                 {
    38.                     if (sonController == 1f)
    39.                     {
    40.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies -= 1f;
    41.                     }
    42.                     gameObject.GetComponentInParent<Control>().accounted1 = true;
    43.                 }
    44.                 if (gameObject.GetComponentInParent<Control>().accounted1 == true)
    45.                 {
    46.                     if (gameObject.GetComponentInParent<Control>().accountGame1 != other.gameObject)
    47.                     {
    48.                         gameObject.GetComponentInParent<Control>().accounted1 = false;
    49.                     }
    50.                 }
    51.             }
    52.             if (borderNumber == 2f)
    53.             {
    54.                 if (gameObject.GetComponentInParent<Control>().accounted2 == false)
    55.                 {
    56.                     if (sonController == 1f)
    57.                     {
    58.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies -= 1f;
    59.                     }
    60.                     gameObject.GetComponentInParent<Control>().accounted2 = true;
    61.                 }
    62.                 if (gameObject.GetComponentInParent<Control>().accounted2 == true)
    63.                 {
    64.                     if (gameObject.GetComponentInParent<Control>().accountGame2 != other.gameObject)
    65.                     {
    66.                         gameObject.GetComponentInParent<Control>().accounted2 = false;
    67.                     }
    68.                 }
    69.             }
    70.             //Destroy(gameObject);
    71.         }
    72.         if (other.gameObject.GetComponent<Control>().controller != sonController)
    73.         {
    74.             Debug.Log("enemy" + other.gameObject);
    75.             if (borderNumber == 0f)
    76.             {
    77.                 if (gameObject.GetComponentInParent<Control>().accounted0 == false)
    78.                 {
    79.                     if (sonController == 1f)
    80.                     {
    81.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies += 1f;
    82.                     }
    83.                     gameObject.GetComponentInParent<Control>().accounted0 = true;
    84.                     gameObject.GetComponentInParent<Control>().accountGame0 = other.gameObject;
    85.                 }
    86.                 if (gameObject.GetComponentInParent<Control>().accounted0 == true)
    87.                 {
    88.                     if (gameObject.GetComponentInParent<Control>().accountGame0 != other.gameObject)
    89.                     {
    90.                         gameObject.GetComponentInParent<Control>().accounted0 = false;
    91.                     }
    92.                 }
    93.             }
    94.             if (borderNumber == 1f)
    95.             {
    96.                 if (gameObject.GetComponentInParent<Control>().accounted1 == false)
    97.                 {
    98.                     if (sonController == 1f)
    99.                     {
    100.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies += 1f;
    101.                     }
    102.                     gameObject.GetComponentInParent<Control>().accounted1 = true;
    103.                     gameObject.GetComponentInParent<Control>().accountGame1 = other.gameObject;
    104.                 }
    105.                 if (gameObject.GetComponentInParent<Control>().accounted1 == true)
    106.                 {
    107.                     if (gameObject.GetComponentInParent<Control>().accountGame1 != other.gameObject)
    108.                     {
    109.                         gameObject.GetComponentInParent<Control>().accounted1 = false;
    110.                     }
    111.                 }
    112.             }
    113.             if (borderNumber == 2f)
    114.             {
    115.                 if (gameObject.GetComponentInParent<Control>().accounted2 == false)
    116.                 {
    117.                     if (sonController == 1f)
    118.                     {
    119.                         GameObject.Find("Spain AI").GetComponent<Country_AI>().borderingEnemies += 1f;
    120.                     }
    121.                     gameObject.GetComponentInParent<Control>().accounted2 = true;
    122.                     gameObject.GetComponentInParent<Control>().accountGame2 = other.gameObject;
    123.                 }
    124.                 if (gameObject.GetComponentInParent<Control>().accounted2 == true)
    125.                 {
    126.                     if (gameObject.GetComponentInParent<Control>().accountGame2 != other.gameObject)
    127.                     {
    128.                         gameObject.GetComponentInParent<Control>().accounted2 = false;
    129.                     }
    130.                 }
    131.             }
    132.             //Destroy(gameObject);
    133.         }
    134.     }
    135.  
    136.     void Start()
    137.     {
    138.         sonController = gameObject.GetComponentInParent<Control>().controller;
    139.     }
    140. }
    141.  
    and here is my Control code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Control : MonoBehaviour
    6. {
    7.     private bool franc;
    8.     private bool spai;
    9.     private bool portuga;
    10.     public float controller;
    11.  
    12.     public float cavalry;
    13.  
    14.     public bool accounted0;
    15.     public bool accounted1;
    16.     [HideInInspector] public bool accounted2;
    17.  
    18.     public GameObject accountGame0;
    19.     public GameObject accountGame1;
    20.     public GameObject accountGame2;
    21.  
    22.     public float goldWorth;
    23.     public float populationWorth;
    24.  
    25.     public List <GameObject> borderSpawn;
    26.     public Vector2 borderPosition0;
    27.     public Vector2 borderPosition1;
    28.     public Vector2 borderPosition2;
    29.  
    30.     void Start()
    31.     {
    32.         StartCoroutine(SpawnBorder(.25f));
    33.         if (controller == 0)
    34.         {
    35.             AddFrance();
    36.         }
    37.         if (controller == 1)
    38.         {
    39.             AddSpain();
    40.         }
    41.         if (controller == 2)
    42.         {
    43.             AddPortugal();
    44.         }
    45.     }
    46.  
    47.     public void ControlChange()
    48.     {
    49.         if (controller == 0)
    50.         {
    51.             AddFrance();
    52.             if (spai == true)
    53.             {
    54.                 RemoveSpain();
    55.             }
    56.             if (portuga == true)
    57.             {
    58.                 RemovePortugal();
    59.             }
    60.         }
    61.         if (controller == 1)
    62.         {
    63.             AddSpain();
    64.             if (franc == true)
    65.             {
    66.                 RemoveFrance();
    67.             }
    68.             if (portuga == true)
    69.             {
    70.                 RemovePortugal();
    71.             }
    72.         }
    73.         if (controller == 2)
    74.         {
    75.             AddPortugal();
    76.             if (franc == true)
    77.             {
    78.                 RemoveFrance();
    79.             }
    80.             if (spai == true)
    81.             {
    82.                 RemoveSpain();
    83.             }
    84.         }
    85.     }
    86.  
    87.     IEnumerator SpawnBorder(float waitTime)
    88.     {
    89.         Instantiate(borderSpawn[0], borderPosition0, Quaternion.identity, transform);
    90.         Instantiate(borderSpawn[1], borderPosition1, Quaternion.identity, transform);
    91.         Instantiate(borderSpawn[2], borderPosition2, Quaternion.identity, transform);
    92.         yield return new WaitForSeconds(waitTime);
    93.         StartCoroutine(SpawnBorder(.25f));
    94.     }
    95.  
    96.     void AddFrance()
    97.     {
    98.         GameObject.Find("information").GetComponent<countries>().franceLand += 1f;
    99.         GameObject.Find("information").GetComponent<countries>().franceGoldWorth += goldWorth;
    100.         franc = true;
    101.     }
    102.     void RemoveFrance()
    103.     {
    104.         GameObject.Find("information").GetComponent<countries>().franceLand -= 1f;
    105.         GameObject.Find("information").GetComponent<countries>().franceGoldWorth -= goldWorth;
    106.         franc = false;
    107.     }
    108.  
    109.     void AddSpain()
    110.     {
    111.         GameObject.Find("information").GetComponent<countries>().spainLand += 1f;
    112.         GameObject.Find("information").GetComponent<countries>().spainGoldWorth += goldWorth;
    113.         spai = true;
    114.     }
    115.     void RemoveSpain()
    116.     {
    117.         GameObject.Find("information").GetComponent<countries>().spainLand -= 1f;
    118.         GameObject.Find("information").GetComponent<countries>().spainGoldWorth -= goldWorth;
    119.         spai = false;
    120.     }
    121.  
    122.     void AddPortugal()
    123.     {
    124.         GameObject.Find("information").GetComponent<countries>().portugalLand += 1f;
    125.         portuga = true;
    126.     }
    127.     void RemovePortugal()
    128.     {
    129.         GameObject.Find("information").GetComponent<countries>().portugalLand -= 1f;
    130.         portuga = false;
    131.     }
    132. }
     
  2. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    The first error is telling you the problem is line 91 where you are trying to access the third element of an array which does not exist
     
  3. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Ok so how would I write it so it ignores it if there is no BorderSpawn[2]. Putting if (BorderSpawn[2] != null) {spawn a prefab} doesn’t work as it pops up the same error
     
  4. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    You are checking an element that does not exist so it’s not null. I can’t read your code on mobile but simply check the size of the array to see if the element exists.
     
  5. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    How would you write that in C#? Sorry I’m still really new to all of this
     
  6. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    You are using a list for border spawn so use borderSpawn.Count to get the number of elements and use an if statement to see if it’s greater or equal to 3 since it’s the 3rd in you are checking.

    Since I cannot see where you add elements to borderSpawn I can only assume the size could be anything between 0 and 3 so you should check to make sure the first 2 elements also need a similar check.
     
  7. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Thank you so much! That perfectly got rid of the errors!! You’ve been a huge help