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

ArgumentOutOfRangeException: Index was out of range.

Discussion in 'Scripting' started by kshitijbagal, Mar 23, 2021.

  1. kshitijbagal

    kshitijbagal

    Joined:
    Oct 20, 2020
    Posts:
    4
    I am trying to create an card game where I first created an database, an card list by which format the data is stored in database And actual card interface with script to extract data from database to shown on card
    but now am getting an error about indexing what can i do to solve this
    upload_2021-3-23_15-40-1.png
    databse script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class cardDatabase : MonoBehaviour
    7. {
    8.     public static List<Cards> cardlist = new List<Cards>();
    9.     void Awoke()
    10.     {//public Cards(int id, string cardname, string cardtype,int level, int attackpower, int deffencepower, string cardDescription, string cardattribute)
    11.         cardlist.Add(new Cards(0,"z1","Monster-Machine",4,1800,2000,"A machine with self awareness","Fire",Resources.Load<Sprite>("0")));
    12.         cardlist.Add(new Cards(1, "King", "Monster-Human", 8, 4000,3500, "A human who's destiny to become King", "Light", Resources.Load<Sprite>("1")));
    13.         cardlist.Add(new Cards(2, "Diablo", "Monster-Demon", 7, 3000,4500, "A the Lord of Terror and one of the Three Prime Evils Prince of Hell", "Dark", Resources.Load<Sprite>("2")));
    14.         cardlist.Add(new Cards(3, "Amaterasu Ōmikami", "Monster-God", 12, 8000,8000, "the Allmighty Sun Goddess ", "Divine", Resources.Load<Sprite>("3")));
    15.         cardlist.Add(new Cards(4, "Hydra", "Monster-Monster", 5, 2200,1200, "a giant snake with nine heads", "Water", Resources.Load<Sprite>("4")));
    16.  
    17.     }
    18. }
    the list which is use to store data in database
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. [ System.Serializable]
    6. public class Cards : MonoBehaviour
    7. {
    8.     public int id;
    9.     public string cardname;
    10.     public string cardtype;
    11.     public int level;
    12.     public int attackpower;
    13.     public int deffencepower;
    14.     public string cardDescription;
    15.     public string cardattribute;
    16.     public Sprite Image;
    17.  
    18.  
    19.     public Cards()
    20.     {
    21.  
    22.     }
    23.     public Cards(int id, string cardname, string cardtype,int level, int attackpower, int deffencepower, string cardDescription, string cardattribute, Sprite Image)
    24.     {
    25.         id = id;
    26.         cardname = cardname;
    27.         cardtype = cardtype;
    28.         level = level;
    29.         attackpower = attackpower;
    30.         deffencepower = deffencepower;
    31.         cardDescription = cardDescription;
    32.         cardattribute = cardattribute;
    33.         Image = Image;
    34.  
    35.  
    36.     }
    37. }
    this is script for extracting data
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class ThisCard : MonoBehaviour
    8. {
    9.     public List<Cards> thisCard = new List<Cards>();
    10.     public int thisId;
    11.  
    12.     public int id;
    13.  
    14.     public string cardname;
    15.     public string cardtype;
    16.     public int level;
    17.     public int attackpower;
    18.     public int deffencepower;
    19.     public string cardDescription;
    20.     public string cardattribute;
    21.     public Sprite SpriteImage;
    22.  
    23.     public Text nameText;
    24.     public Text typeText;
    25.     public Text levelText;
    26.     public Text attackText;
    27.     public Text deffenceText;
    28.     public Text DescriptionText;
    29.     public Text attributeText;
    30.     public Image thatImage;
    31.  
    32.     void Start()
    33.     {
    34.         thisCard [0] = cardDatabase.cardlist[thisId];
    35.  
    36.     }
    37.     void Update()
    38.     {
    39.         id = thisCard[0].id;
    40.         cardname = thisCard[0].cardname;
    41.         cardtype = thisCard[0].cardtype;
    42.         level = thisCard[0].level;
    43.         attackpower = thisCard[0].attackpower;
    44.         deffencepower = thisCard[0].deffencepower;
    45.         cardDescription = thisCard[0].cardDescription;
    46.         cardattribute = thisCard[0].cardattribute;
    47.         SpriteImage = thisCard[0].Image;
    48.  
    49.         nameText.text = "" + cardname;
    50.         typeText.text = "" + cardtype;
    51.         levelText.text = "" + level;
    52.         attackText.text = "" + attackpower;
    53.         deffenceText.text = "" + deffencepower;
    54.         DescriptionText.text = "" + cardDescription;
    55.         attributeText.text = "" + cardattribute;
    56.         thatImage.sprite= SpriteImage;  }
    57. }
    58.  
    Error at
    Code (CSharp):
    1. void Start()
    2.     {
    3.         thisCard [0] = cardDatabase.cardlist[thisId];
    4.  
    5.     }
    thanks for help
     

    Attached Files:

    Last edited: Mar 23, 2021
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
  3. kshitijbagal

    kshitijbagal

    Joined:
    Oct 20, 2020
    Posts:
    4
    davidnibi likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,760
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236
     
    kshitijbagal likes this.
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Your card database has a function called Awoke, this should be Awake
     
  6. Xprometeo

    Xprometeo

    Joined:
    Mar 23, 2021
    Posts:
    7
    Hi, i am following Cezary Sharp too, in his video: "Unity Card Game; Scriptable Object Card C# @02" in min 7:35 you can see how he edit in inspector "this card" script changing "size" from 0 to 1. That is what you need to solve this, the problem is... what i can,t see "size" in the inspector, in am asking him in facebook, but no response in 3 days, probably a lot people here know how edit the hidden parameter "size" in a script.
     
    kshitijbagal likes this.