Search Unity

UnassignedReferenceException: The variable cardAtlas of LoadCards has not been assigned.

Discussion in 'Scripting' started by pKallv, Feb 3, 2019.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    What am I missing here, I think I have assigned the atlas:

    Skärmavbild 2019-02-03 kl. 21.13.25.png

    Skärmavbild 2019-02-03 kl. 21.14.07.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.U2D;
    6.  
    7. public class LoadCards : MonoBehaviour
    8. {
    9.  
    10.     private CardsAndConversions _cardConvert;
    11.  
    12.     public GameObject templateCard_GO;
    13.     public Image templateCard;
    14.  
    15.     public SpriteAtlas cardAtlas;
    16.     private SpriteRenderer spriteRenderer;
    17.     private SpriteType cardSprite;
    18.  
    19.     // DEFINITION OF WHAT IS IN THE SPRITE ATLAS FOR CARDS
    20.     enum SpriteType
    21.     {
    22.         BackSide1, BackSide2, BackSide3, BackSide4,
    23.         CA, C2, C3, C4, C5, C6, C7, C8, C9, C10, CJ, CQ, CK,
    24.         DA, D2, D3, D4, D5, D6, D7, D8, D9, D10, DJ, DQ, DK,
    25.         SA, S2, S3, S4, S5, S6, S7, S8, S9, S10, SJ, SQ, SK
    26.     }
    27.  
    28.  
    29.     void Start()
    30.     {
    31.  
    32.         _cardConvert = FindObjectOfType(typeof(CardsAndConversions)) as CardsAndConversions;
    33.         spriteRenderer = GetComponent<SpriteRenderer>();
    34.         print(cardAtlas.name);
    35.  
    36.         LoadCardDeck();
    37.     }
    38.  
    39.     void LoadCardDeck ()
    40.     {
    41.         float u = 0;
    42.         foreach (string _name in CardsAndConversions.backFullCardDeck)
    43.         {
    44.             GameObject myCard = Instantiate(templateCard_GO) as GameObject;
    45.             templateCard = myCard.GetComponent<Image>();
    46.             spriteRenderer.sprite = cardAtlas.GetSprite("BackSide");
    47.             templateCard.sprite = spriteRenderer.sprite;
    48.             templateCard.rectTransform.SetParent(transform);
    49.             templateCard.rectTransform.anchoredPosition = new Vector2(0f, u);
    50.             templateCard.name = _name;
    51.             u = u + 10f;
    52.         }
    53.     }
    54. }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    "There is no sprite renderer attached to the background game object" is not referring to the sprite atlas variable. Is that screenshot the correct error? If so, the issue is that your trying to get a sprite renderer where there isn't one.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    ofc how could I miss that, thanks.
     
    Last edited: Feb 8, 2019
    MD_Reptile likes this.
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    It happens to us all sometimes :p

    Good luck!
     
    pKallv likes this.