The question is answered, right answer was accepted
How to add to an int in another script?
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++;
}
}
}
Follow this Question
Related Questions
Adding Multiple Tags to a GameObject[] Array 0 Answers
Any reason why this method of adding gameobjects with this tag to an array won't work? 0 Answers
Background Sprites lose their tags when the project is built 0 Answers
How can i add multiple tags from script? 1 Answer
Way of Finding/accessing a variable from another script though another variable? 1 Answer