Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI Image won't change

Discussion in 'Scripting' started by Cylibus, Jun 25, 2021.

  1. Cylibus

    Cylibus

    Joined:
    Mar 29, 2014
    Posts:
    6
    So I decided to try to make a small card game for a game jam, only it's something I've never quite done before. I've thought of a few methods to go about it, but since it's a game jam I decided to go with a straightforward method of placing UI components in the spots where a card will be and just having a script to change the UI components (the card title, the card image, and the card description).

    My main problem right now is with changing the card image. The way I have it set is I have a deck script that will draw the card and determine the card id number which is passed into a method on my Card class called "SetValues" which determines what title, image, and description the new card should have. I've been testing it, and I've gotten the title and description to change to what I want it to, but the image remains blank. I'm not getting any kind of error message saying that something is incorrect, and so far I've tried switching between using Resources.Load and using serialized fields to store the images in case that was the issue. I've tried switching between using an image component and using a sprite renderer component. I've also tried changing from editing the image.sprite to editing the sprite.override and neither is working. I've even tried using tags instead of Gameobject.find but neither seemed to work. If I look in the inspector panel upon playing, the source image of the image component does not change when playing so it's not actually updating the image at all. Here's what my Card class looks like (warning ahead of time that I'm in the process of working through functionality first and making the code efficient and good looking later)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Card {
    7.  
    8.    
    9.  
    10.     public string title;
    11.     public string description;
    12.     public Sprite image;
    13.  
    14.     Text cardTitle;
    15.     Text cardDescription;
    16.     Image cardImage;
    17.  
    18.     Sprite card1Image;
    19.     Sprite card2Image;
    20.     Sprite card3Image;
    21.     Sprite card4Image;
    22.     Sprite card5Image;
    23.     Sprite card6Image;
    24.     Sprite card7Image;
    25.     Sprite card8Image;
    26.     Sprite card9Image;
    27.     Sprite card10Image;
    28.  
    29.     #region Icons
    30.     public void Awake()
    31.     {
    32.         card1Image = Resources.Load<Sprite>("1");
    33.  
    34.         card2Image = Resources.Load<Sprite>("2");
    35.  
    36.         card3Image = Resources.Load<Sprite>("3");
    37.  
    38.         card4Image = Resources.Load<Sprite>("4");
    39.  
    40.         card5Image = Resources.Load<Sprite>("5");
    41.  
    42.         card6Image = Resources.Load<Sprite>("6");
    43.  
    44.         card7Image = Resources.Load<Sprite>("7");
    45.  
    46.         card8Image = Resources.Load<Sprite>("8");
    47.  
    48.         card9Image = Resources.Load<Sprite>("9");
    49.  
    50.         card10Image = Resources.Load<Sprite>("10");
    51.     }
    52.     #endregion
    53.  
    54.     public void setValues(int cardNumber)
    55.     {
    56.      
    57.         switch (cardNumber)
    58.         {
    59.             case 1:
    60.                 title = "Card 1";
    61.                 description = "This is the first card";
    62.                 image = card1Image;
    63.                 break;
    64.  
    65.             case 2:
    66.                 title = "Card 2";
    67.                 description = "This is the second card";
    68.                 image = card2Image;
    69.                 break;
    70.  
    71.             case 3:
    72.                 title = "Card 3";
    73.                 description = "This is the third card";
    74.                 image = card3Image;
    75.                 break;
    76.  
    77.             case 4:
    78.                 title = "Card 4";
    79.                 description = "This is the fourth card";
    80.                 image = card4Image;
    81.                 break;
    82.  
    83.             case 5:
    84.                 title = "Card 5";
    85.                 description = "This is the fifth card";
    86.                 image = card5Image;
    87.                 break;
    88.  
    89.             case 6:
    90.                 title = "Card 6";
    91.                 description = "This is the sixth card";
    92.                 image = card6Image;
    93.                 break;
    94.  
    95.             case 7:
    96.                 title = "Card 7";
    97.                 description = "This is the seventh card";
    98.                 image = card7Image;
    99.                 break;
    100.  
    101.             case 8:
    102.                 title = "Card 8";
    103.                 description = "This is the eigth card";
    104.                 image = card8Image;
    105.                 break;
    106.  
    107.             case 9:
    108.                 title = "Card 9";
    109.                 description = "This is the ninth card";
    110.                 image = card9Image;
    111.                 break;
    112.  
    113.             case 10:
    114.                 title = "Card 10";
    115.                 description = "This is the tenth card";
    116.                 image = card10Image;
    117.                 break;
    118.         }
    119.  
    120.         cardTitle = GameObject.Find("Card 1 Title").GetComponent<Text>();
    121.         cardDescription = GameObject.Find("Card 1 Description").GetComponent<Text>();
    122.         cardImage = GameObject.Find("Card 1 Image").GetComponent<Image>();
    123.        cardTitle.text = title;
    124.         cardDescription.text = description;
    125.         cardImage.sprite = image;
    126.     }
    127. }
    128.  
     
  2. kpprt

    kpprt

    Joined:
    Jul 30, 2016
    Posts:
    11
    Hi @Cylibus . Do you get any errors? And do you actually call the SetValues function? There is no example code on how the class is used so I'm just asking the simple questions first.
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Have you tried using a direct reference to the GameObject instead of using .Find() on everything?
     
  4. Cylibus

    Cylibus

    Joined:
    Mar 29, 2014
    Posts:
    6
    Yes, the SetValues function is called from another class upon starting the game, and then called again with every press of the spacebar. And since the title and description do update I can confirm that the function is running from that. And no, no errors showing up to give me any hint of where the issue might be.

    Yes, I've tried using a serialized field approach first so that I could just place the images in individually on the inspector, but I still end up with the same problem.
     
  5. kpprt

    kpprt

    Joined:
    Jul 30, 2016
    Posts:
    11
    That is indeed a bit strange. Maybe you could try to set the Image dirty with SetAllDirty or Rebuild. I don't think that should be necessary and it's also not in that simple example, but maybe it's worth a try anyway. Otherwise the only thing I can see that might still miss are the references to the new sprites, but I guess you checked that already.
     
  6. Cylibus

    Cylibus

    Joined:
    Mar 29, 2014
    Posts:
    6
    So an update on this, I wasn't able to find out where the issue was, but I made a completely new script and had it load and apply the image in the same way and it worked, so I decided to scrap the script and start again from scratch. Luckily it wasn't that complicated of a script that I can just rewrite it.