Script error

Hey im not quite sure this is the right forum to ask but i have have a problem with my code and i cant figure out
whats wrong so maybe you can help me out.

I try to make a card game and wanted to code that the player draws card from its deck, i used a Tutorial and double checked everything but everything seems just like the code in the Tutorial

here are the Error messages

Assets\Scripts\ThisCard.cs(48,31): error CS0120: An object reference is required for the non-static field, method, or property ‘PlayerDeck.deckSize’

Assets\Scripts\ThisCard.cs(159,13): error CS0120: An object reference is required for the non-static field, method, or property ‘PlayerDeck.deckSize’

here is the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ThisCard : MonoBehaviour
{
    public List<Card> thisCard = new List<Card>();
    public int thisId;

    public int id;
    public string cardName;
    public int cost;
    public string element;
    public int power;
    public string race;
    public string effekt2;
    public string cardDescription;

    public Sprite thisSprite;
    public Image thatImage;

    public GameObject legendar;
    public GameObject fly;
    public GameObject bow;

    public Image frame;
    public Image rarity;

    public Text attackText;
    public Text nameText;
    public Text raceText;
    public Text effektText;

    public bool cardBack;
    public static bool staticCardBack;

    public GameObject Hand;

    public int numberOfCardsInDeck;


    void Start ()
    {

        thisCard [0] = CardDataBase.cardList[thisId];
        numberOfCardsInDeck = PlayerDeck.deckSize;
      
    }

    void Update ()
    {

        Hand = GameObject.Find("PlayerHand");
        if(this.transform.parent == Hand.transform.parent)
        {
            cardBack = false;
        }

        id = thisCard[0].id;
        power = thisCard[0].power;
        cardName = thisCard[0].cardName;
        race = thisCard[0].race;
        effekt2 = thisCard[0].effekt2;

        thisSprite = thisCard[0].thisImage;

        attackText.text = " " + power;
        nameText.text = " " + cardName;
        raceText.text = " " + race;
        effektText.text = " " + effekt2;

        thatImage.sprite = thisSprite;

        // Elements
        if(thisCard[0].element=="Fire")
        {
            frame.GetComponent<Image>().color = new Color32(212, 36, 0, 255);
        }

        if (thisCard[0].element == "Wind")
        {
            frame.GetComponent<Image>().color = new Color32(43, 255, 245, 255);
        }

        if (thisCard[0].element == "Earth")
        {
            frame.GetComponent<Image>().color = new Color32(0, 171, 2, 255);
        }

        if (thisCard[0].element == "Water")
        {
            frame.GetComponent<Image>().color = new Color32(13, 82, 125, 255);
        }

        if (thisCard[0].element == "Light")
        {
            frame.GetComponent<Image>().color = new Color32(250, 255, 10, 255);
        }

        if (thisCard[0].element == "Darkness")
        {
            frame.GetComponent<Image>().color = new Color32(80, 80, 80, 255);
        }


        //Rarity
        if (thisCard[0].seltenheit == "common")
        {
            rarity.GetComponent<Image>().color = new Color32(232, 0, 255, 255);
        }
        if (thisCard[0].element == "rare")
        {
            frame.GetComponent<Image>().color = new Color32(135, 0, 255, 255);
        }
        if (thisCard[0].element == "legendary")
        {
            frame.GetComponent<Image>().color = new Color32(255, 160, 0, 255);
        }


        //Symbols
        if (thisCard[0].legendary == true)
        {
            legendar.SetActive(true);
        }
        else
        {
            legendar.SetActive(false);
        }

        if (thisCard[0].flying == true)
        {
            fly.SetActive(true);
        }
        else
        {
            fly.SetActive(false);
        }

        if (thisCard[0].range == true)
        {
            bow.SetActive(true);
        }
        else
        {
            bow.SetActive(false);
        }

        //Card Back
        staticCardBack = cardBack;


        if(this.tag == "Clone")
        {
            thisCard[0] = PlayerDeck.staticDeck[numberOfCardsInDeck - 1];
            numberOfCardsInDeck -= 1;
            PlayerDeck.deckSize -= 1;
            cardBack = false;
            this.tag = "Untagged";
        }



    }

}

thanks in advance and sorry for the spelling errors

Your errors tell you what line to go to. Line 48 and 159.
I’m just going to say, you need to check that tutorial again. Either they change something a little further in it, or you definitely didn’t match everything.

You should be able to Google that error and get a ton of results since it’s a pretty common basic error, but I’ll tell you what it means. Basically, PlayerDeck.deckSize is not static which means you need an instance of PlayerDeck that you would reference instead and get the deckSize from that.

That means either you are missing creating a reference and instance of PlayerDeck or deckSize should be static, but I’m not doing the tutorial, so you’ll need to figure out which it is.

Hello, your issue is in the PlayerDeck class, the field deckSize isn’t declared static so you can’t access it simply calling PlayerDeck.deckSize.

possibilities, either the value of deck size is the same form all players then you declare the field as static and that’s all, or it’s something that change from one player to the other and then in this class you need to pass an instance of the PlayerDeck object and expose a getter to deckSize value.

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

Perfect thank you im not quite sure how the guy in the tutorial did it without the static declaration but it worked for me so far i cant say how gratitude i am and thanks for the link i will check it out

The confusion on this is often because C# actually allows you to re-use the same name as the class, when you make a public reference field to it. Personally I think this is dreadful practice because it visually obscures what is going on.

If you have a class called KurtScript and you make a public field out of it:

public KurtScript KurtScript;   // perfectly legal, but needlessly confusing

Then later on you can go:

KurtScript.DoSomething();

And now it kinda takes some thinking (or syntax coloring memory) to reason about if you are using a static method or an instance method. Only one can be correct. :slight_smile:

1 Like