Click here to Skip to main content
15,944,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I coded it so it displays a card, exept it doesnt,
The code:
   public List<card> displayCard = new List<card>();
    public int displayId;

    public int id;
    public string cardName;
    public int cost;
    public int power;
    public string cardDescription;

    public Text nameText;   
    public Text costText;
    public Text powerText;
    public Text descriptionText;


    // Start is called before the first frame update    
    void Start()
        displayCard[0] = CardDatabase.cardList[displayId];

    // Update is called once per frame
    void Update()
         id = displayCard[0].id;
         cardName = displayCard[0].cardName;
         cost = displayCard[0].cost;
         power = displayCard[0].power;
         cardDescription = displayCard[0].cardDescription;

       nameText.text = " " + cardName;
       costText.text = " " + cost;
       powerText.text = " " + power;
       descriptionText.text = " " + cardDescription;

I have the cards on a different script

What I have tried:

Changing the card id etc. Rewriting it incase i forgot something, idk what esle to do
Member 16187429 22-Jan-24 14:09pm    
Also im using unity btw
Member 16187429 22-Jan-24 15:01pm    
I figured it out

This is just a guess from what little code you've shown here. I assume your error occurs in method Start since it seems to be the first thing called according to your problem description. Use the debugger and place a breakpoint at the error indicated line to see exactly what is going on.

At the top of your code you've created a new List (displayCard) that can hold some number "card" objects (the default capacity). But, at this point it is empty and has no cards in it.

In method Start (and in Update) you try to use the card object at index 0 of displayCard - but there are no cards in it so 0 is out of range. You need to put a card (Add method is one way) in the list first.

Again this is just a guess because I see no code that adds a card object to displayCard before you try to use index 0. If there is code to do this, use the debugger to find out what is happening. Valuable tools those debuggers, take it from someone who started out on a system without one.
Share this answer
CPallini 23-Jan-24 2:29am    
Just to add to what FreedMalloc has said, an array or ind3exable collection like a List<T> containing N elements has valid index values from 0 to N - 1.
So a List<int> that contains three values
List<int> myNumbers = new List<int>() {10, 20, 30};
Would have three valid indexes: 0, 1, and 2 - any other value (positive or negative) will give you an "Index out of range" exception

Since your code explicitly uses an index of 0, and you get an out of range exception that means your collection is empty: it has no elements so it has no valid indexes.

The solution to that is to add at least one card to the collection!

But ... your code seems really muddled: I have no idea what class the code you show is in, but there are two possibilities:
1) That code is part of the card class. If so, then it shouldn't be accessing display components like nameText, costText, powerText, and descriptionText because they are part of your form, not the card.

2) That code is part of your form. If so, then it shouldn't have card related values like cardName, cost, power, and cardDescription because they are part of the individual card and should be contained there rather than duplicated.

Have a think about what you are trying to do: I suspect you are a little hazy on the concept of classes and instances, as well as collections! :D
Share this answer
CPallini 23-Jan-24 2:29am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900