- Home /
(UNO card game) I can't get the data with GetComponent function for an instance of an object
Hi! This is kind of hard to explain but I'll do my best.
I'm making a UNO card like game, at starting the game it spawns a random card, with a random number an color and then you have to place a matching card there, but when trying to store the data for the number of that initial card in another variable, it doesn't detect it.
I have two functions:
InitialCard() spawns the first card and try to obtain it's data (doesn't obtain it)
DropCard() is when the player drags a card and dop it above the first or previous one and it works, the Debug shows the number of that card
So if in the DropCard() works, why doesn't work in InitialCard() that is exactly the same and should work? Please help.
(the code below is sumarized, I only show the relevant part, I erased some things but doesn't affect the situation)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlaceArea : MonoBehaviour{
[HideInInspector]
public int placedNumber;
//Cards
[HideInInspector]
public GameObject currentCard = null;
[HideInInspector]
public GameObject newCard = null;
void Start()
{
InitialCard();
}
//Here is where the first card appears with his own random number and try to obtain his number data in the variable "placedNumber"
void InitialCard()
{
//initial card instance and position
currentCard = Instantiate(manager.initialCard, new Vector3(0, 0, 0), Quaternion.identity);
currentCard.transform.SetParent(gameObject.transform, false);
//initial card data (*ERROR HERE*)
placedNumber = currentCard.GetComponentInChildren<CardsData>().cardNumber;
Debug.Log("card data: " + placedNumber);
}
//Here is where it tries to obtain the data for all the new cards that are dropped above the first card ^^ and it does works perfectly, it can get the number data of that card in "placedNumber" but in the first one it can't
public void DropCard()
{
//new card data (works fine)
placedNumber = newCard.GetComponentInChildren<CardsData>().cardNumber;
cardIsPlaced = true;
Debug.Log("card data: " + placedNumber);
Replace(currentCard, newCard);
image.color = normalColor;
}
}
This is what happens graphically: If something isn't clear please tell me and I would nicely clarify anything. Is there anyone else trying to make a UNO like game or any card game?
Your answer
![](https://koobas.hobune.stream/wayback/20220613012115im_/https://answers.unity.com/themes/thub/images/avi.jpg)