Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Bug while changing my text with script

Discussion in 'UGUI & TextMesh Pro' started by MangeCactus, Jan 29, 2023.

  1. MangeCactus

    MangeCactus

    Joined:
    Jul 29, 2022
    Posts:
    1
    I am trying to make a game of cards, while instantiating a card i want it to take so parametere like the mana, the damage etc, witch it does, and then update the text with those number. The probleme is that the console return this : NullReferenceException: Object reference not set to an instance of an object
    Please help me

    Here is the code of the instantiation

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class CardController : MonoBehaviour
    8. {
    9.     public Card card;
    10.     public Image illustrationB;
    11.     public TextMeshProUGUI cardNameB, healthB, damageB, manaCostB;
    12.  
    13.  
    14.     public void Initialize(Card card)
    15.     {
    16.         this.card = card;
    17.         cardNameB.text = card.cardName; //this doesnt work apparently, and the following lines too
    18.         manaCostB.text = card.manaCost.ToString();
    19.         damageB.text = card.damage.ToString();
    20.         healthB.text = card.health.ToString();
    21.         illustrationB.sprite = card.illustration;
    22.  
    23.     }
    24. }
    And to generate the cards, here is the methode
    Code (CSharp):
    1. public void GenerateCards()
    2.     {
    3.         foreach (Card cardElement in cards)
    4.         {
    5.             CardController newCard = Instantiate(cardControllerPrefab, player1Hand);
    6.             newCard.transform.localPosition = Vector3.zero;
    7.             newCard.Initialize(cardElement);
    8.         }
    9.     }
    the list is not empty so i dont understand why it's not working, if you have any ideas or need more info feel free to ask me. thank!