Trying to and to an int but I don't know why it isn't working

I am making an electronic card game and I made a script that is responable for creating the deck and spawning them. I have another script for despawning cards and add to the counter when they become a child. The problem is the code will not add to the counter of the other script when the gameobject becomes a child to it. Do you know a way to make the counter go up when the gameobject becomes a child?

This is the code that is respponsable for spawning cards:

public class cardSpawner : MonoBehaviour, IPointerDownHandler {

    [SerializeField] GameObject redCard;

    [Header("Where Cards Spawn")]
    [SerializeField] Transform palette;

    [SerializeField]List<GameObject> gameDeck = new List<GameObject>();
    //This is the counter the other program will add to.
    [SerializeField] public int amountColorCard = 9;

    void Start() {
        FillDeck();
    }

    void Update() {
        if (gameDeck.Count == 0) {
            FillDeck();
        }
    }

    void FillDeck() {
        while (amountColorCard > 0) {
            gameDeck.Add(redCard);
            amountColorCard--;
        }
    }

    public void OnPointerDown(PointerEventData eventData) {
        if (palette.transform.childCount >= 6) { return; }
        else {
            CardChooser(); 
        }
    }

    void CardChooser() {
            int coolNewNumber = UnityEngine.Random.Range(0, gameDeck.Count);
            Instantiate(gameDeck[coolNewNumber], palette);
            gameDeck.Remove(gameDeck[coolNewNumber]);
    }
}

This is the code for despawning and adding to the code in the first script.

public class DiscardPile : MonoBehaviour {

    public Transform discardPile;
    public GameObject childness;

    private cardSpawner CardSpawner;

    void Update() {
        if (gameObject.transform.childCount >= 2) {
            Debug.Log("Too Many Objects!");
            DestroyChild();
        }
    }

    void DestroyChild() {
        discardPile = gameObject.transform;
        IncreaseCardPile();
        childness = gameObject.transform.GetChild(0).gameObject;
        Destroy(childness);
    }

    void IncreaseCardPile() {
        if (discardPile.GetChild(1).tag == "Red") {
            CardSpawner.amountColorCard++;
        } 
    }
}```

Hi,

Your variable CardSpawner isn’t initialised anywhere in in the _DiscardPile`. It has no reference to the class you want to update the variable in.

If you only have on cardSpawner in your game you could grab a reference to it in DiscardPile using FindObjectOfType<cardSpawner>() in perhaps a Start method.

Side note : your capitalisation for the cardSpawner class/variable is inconsistent, makes it a bit harder to read your code.

Thanks for the help. It now works as it should.

I’m still trying to understand what is best to capitalize. It’s a lot harder than I thought it would be.

Have a look at http://wiki.unity3d.com/index.php/Csharp_Coding_Guidelines

As you learn to stick to coding standards, you will find your code is easier to read and more understandable

Classes and Methods (Functions) should be capitalised

You can see the standard Unity classes are capitalised,

public Transform discardPile;
public GameObject childness;

so yours should be too

private CardSpawner cardSpawner;

(you shouldn’t need to use SerializeField on public variables by the way)

When I look at this line, applying coding standards, I can already see an issue

CardSpawner.amountColorCard++;
  1. CardSpawner capitalised is the class, so I’d expect a static variable after (but that’s not how you are using it). See the example on statics here https://unity3d.com/learn/tutorials/topics/scripting/statics?playlist=17117

Also at some point when you study code you will start to see differences between private fields (not capitalised) and public properties (capitalised)

(Note the basic types such as int, string etc are not capitalised though)

class Person
{
    private string name;  // the name field
    public string Name    // the Name property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

Person person = new Person();
person.Name = "Joe";  // the set accessor is invoked here                

System.Console.Write(person.Name);  // the get accessor is invoked here

See here for a better example https://unity3d.com/learn/tutorials/topics/scripting/properties?playlist=17117

I’m not saying public variables should always be capitalised , but public properties should.

You’ll see that prior to introducing properties, they do use public variables with lowercase

public int alpha = 5;

But it is useful to understand the difference

1 Like

Privacy & Terms