I am working on TCG (trading card game) project and working on the dragging for the card and it's all working except for the parent which the card prefab should return to, in the editor it shows that the card prefab returns to main screen even thou it should go back to its original place in the player1hand area, there are no errors shown in C# code but there is one in unity cosole
Here is error in the editor's console
enter image description here
Here is the CardController class
public class CardController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler,IDragHandler {
//creating a special variable with card type
public Card card;
//creating a 2 variables with Image type for the card sprite and playerhand background
public Image character, image;
//creating a 3 variables with TextMeshProUGUI type for the card stats
public TextMeshProUGUI cardName, attack, defence, level, description;
//dont know what to write plz help
private Transform originalParent;
//dont know what to write plz help
private void Awake()
{
image = GetComponent<Image>();
}
//assgining the data from Card class to this class
public void Initialize(Card card)
{
this.card = card;
character.sprite = card.character;
cardName.text = card.cardName;
attack.text = card.attack.ToString();
defence.text = card.defence.ToString();
level.text = card.level.ToString();
description.text = card.description;
originalParent = transform.parent;
}
//what should happen when releasing the card
public void OnPointerUp(PointerEventData eventData)
{
//dont know what to write plz help
image.raycastTarget = true;
//returning the card to his original position
transform.SetParent(originalParent);
//to center the card in the middle of the parent
transform.localPosition = Vector3.zero;
}
//what should happen when holding the card
public void OnPointerDown(PointerEventData eventData)
{
//changing the position of the card when holding left click
transform.SetParent(transform.root);
//dont know what to write plz help
image.raycastTarget = false;
}
//what should happen when dragging the card
public void OnDrag(PointerEventData eventData)
{
//changing the position of the card to the new position of the mouse while holding left click
transform.position = eventData.position;
}}
Here is CardManager class
public class CardManager : MonoBehaviour {
// making CardManager class gobal to access
public static CardManager instance;
// to create a list of cards
public List<Card> cards = new List<Card>();
// to declare the place where cards are summoned
public Transform player1Hand, player2Hand;
// creating a cardControllerPrefab varible to hold the CardPrefab from the editor
public CardController cardControllerPrefab;
//summoning CardController class in CardManager class
private void Awake()
{
instance = this;
}
//to start the GenerateCard function when the game starts
private void Start()
{
GenerateCards();
}
//to generate the cards using foreach loop and putting them in place
private void GenerateCards()
{
foreach(Card card in cards)
{
//to create a new card using CardController class and putting it in CardControllerPrefab
CardController newCard = Instantiate(cardControllerPrefab, player1Hand);
//to put the card in the player1Hand Area
newCard.transform.localPosition = Vector3.zero;
//to actavite the Card data class using a code in the CardController class
newCard.Initialize(card);
}
}}
Note if also you can help me improve my commenting I would be so thankful
CodePudding user response:
When Unity hits an exception it bails on the current method but otherwise continues to operate (instead of crashing to desktop).
You haven't told us what's on line 25, but it's preventing your code from getting to the following line:
originalParent = transform.parent;
You're not getting to set this (because you abort when you hit the error on line 25), so it's null when you go to use it. A null transform parent is a totally valid option, though - it means the transform has no parent (and should be a root-level game object).
Best advice would be to fix your null reference exception, but short of that you could move the originalParent line to the top of the Initialize method.
CodePudding user response:
Unity gets the parent object:
GameObject fu = this.transform.parent.gameObject; //Get the parent object of the current object
GameObject fufu = fu.transform.parent.gameObject;// Get the parent object of the specified object
Hope it helps you.