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

Changing an image within a panel in realtime.

Discussion in 'Scripting' started by ruhlmcu, Sep 9, 2020.

  1. ruhlmcu

    ruhlmcu

    Joined:
    Dec 30, 2019
    Posts:
    7
    I am would like to change the image on a panel each time the user clicks a restart button on a second panel. The restart function and panel display are working fine. However I have been unable to successfully change the image.

    the code I have implemented to rebuild and redisplay the panel is:

    Code (CSharp):
    1.  
    2.  
    3. private void buildPromptPanel()
    4.     {
    5.         //get the next or a random sprite from the directory and assign it to newSprite
    6.         System.Random rnd = new System.Random();
    7.         int nxt = rnd.Next(0, 3);  // creates a number between 0 and 3
    8.         var newSprite = imageList[nxt];
    9.         message = ("random number " + nxt);
    10.         messagetoConsole(message);
    11.         message = ("new Sprite is " + newSprite);
    12.         messagetoConsole(message);
    13.         //override existing sprite with newSprite
    14.         //promptPanel.image.overrideSprite = newSprite;
    15.         promptPanel.SetActive(true);
    16.     }
    17.  
    The panel hierarchy looks like this:

    PromptPanel
    checkworkButton
    Text​
    Description
    promptImage
    Can someone help me understand how to change promptImage in PromptPanel? If more code would be useful please let me know.

    Thanks in advance for your help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    What happens when you try?
    Are you running into compile issues?
    Are you getting errors at runtime?

    What type of collection is
    imageList
    ? Is it a collection of Sprites? Texture2Ds?
     
  3. ruhlmcu

    ruhlmcu

    Joined:
    Dec 30, 2019
    Posts:
    7
    @PraetorBlue thanks for the quick response.

    I get a compile errors on the different updates I've tried. Each error is different depending on what I tried. The error on the commented line in the code
    Code (CSharp):
    1. promptPanel.Panelimage.overrideSprite = newSprite;
    is:

    Assets/Scripts/PlacementAndLaunchingCanvasController.cs(135,21): error CS1061: 'GameObject' does not contain a definition for 'Panelimage' and no accessible extension method 'Panelimage' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

    Code (CSharp):
    1. imageList = Resources.LoadAll<Sprite>("sprites/");
    This is what is loading imageList. It seems to work fine on some console logging I have done.

    Thanks for any advice you might have.
     
  4. ruhlmcu

    ruhlmcu

    Joined:
    Dec 30, 2019
    Posts:
    7
    Update. There was a typo in the code above and therefore the error message. The problem is the same but I wanted to give accurate information. The code should have read:

    Code (CSharp):
    1. promptPanel.promptImage.overrideSprite = newSprite;
    The error is:

    Assets/Scripts/PlacementAndLaunchingCanvasController.cs(136,21): error CS1061: 'GameObject' does not contain a definition for 'promptImage' and no accessible extension method 'promptImage' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    If you want to get a component attached to a GameObject you have to use the GetComponent method:

    Code (CSharp):
    1. promptPanel.GetComponent<Panelimage>().overrideSprite;
    Just make sure the part inside the <> brackets is the name of your MonoBehaviour class.
     
  6. ruhlmcu

    ruhlmcu

    Joined:
    Dec 30, 2019
    Posts:
    7

    Thanks for the help. The issue still exists but I think it has to do with how I have the panel and items on the panel defined. I will need to work with that for a while. I am sure I'll be back with more questions.
     
  7. ruhlmcu

    ruhlmcu

    Joined:
    Dec 30, 2019
    Posts:
    7
    @PraetorBlue again, thanks for the help. I did solve this- although I am not satisfied with what I perceive as a workaround. My initial panel design had a solid background as the "image" component of the panel. Within that panel I added a UI Image object. This second image is the one I wanted to change.

    Screen Shot 2020-09-10 at 8.25.17 AM.png Screen Shot 2020-09-10 at 8.15.45 AM.png


    I was unable to "find" or "address" this second image so I changed my panel. The second image is now a component of the panel but I do not have a solid background.

    Screen Shot 2020-09-10 at 8.20.58 AM.png Screen Shot 2020-09-10 at 8.12.55 AM.png