Get image to show on screen

So I have a weather system, but since its so early on i just want to show an icon that relates to the type of weather is currently active.
How would i show this image and have it change each time the weather changes?

I understand i can just put it in the weather system code, but i dont actually know the code to make the image/sprite show

You could start on this UI tutorial. Adding an image isn’t in there, but you just get it from the menu and drag your image on there.
https://noobtuts.com/unity/ui

Doesn’t really cover what i am looking for. Im just looking for a piece of code that shows an image/sprite, that i can activate when a certain weather state is active

This is how you do it:

In the canvas ui scheme, there is an UI Element called Raw Image. You can use that and put it in a canvas/panel under it.

Next, on the event of the weather change, change the mainTexture property of the rawImage:

References:

https://docs.unity3d.com/Manual/script-RawImage.html

https://docs.unity3d.com/ScriptReference/UI.RawImage.html

Cheers!

But how do i change the maintexture?

Ive only been using unity for a few weeks so struggling with understanding a few things

Thanks

If you are using Unity.UI
You can use Canvas.FindObjectOfType()
or you can get an array with FindObjectsOfType

Check this link I think this will help you. You need to have different skyboxes for different weather. And you can change them whenever you want during your gameplay.

Sorry that I am being such an idiot about this.
So I have my weather code i would need to declare each image, then in the weather switch statement use the Canvas.FindObjectOfType() ?

I create the canvas and images using the editor. The problem with that is it’s only good for that scene. What you are doing probably means you want it for many scenes, so creating it with code might be better.
I haven’t done that. You can generally do a search for whatever you are trying to do with the word “unity” first and get a lot of relevant answers.
This turned up when I looked:
http://answers.unity3d.com/questions/849176/how-to-create-a-canvas-and-text-ui-46-object-using.html

However, personally, I would create a canvas and images in the editor for the scene for now maybe, just to get used to using that system.
Basically you just go to GameObject in the menu screen, go down to UI, and then Canvas. To the canvas you add an image doing exactly the same thing, actually 2 images. They will just be white squares. You drag your image onto those squares.

Then for code you do like I said from any other script, but you create an array first :

using UnityEngine.UI;
class name{
Image[] images;
void Start(){
images = Canvas.FindObjectsOfType<Image>();
}
}

Do that with both images enabled, because it won’t find them if they are disabled. Then you enable or disable them by using:

void functionName(){
images[0].enabled = false;
images[1].enabled = true;
}

Like I say, do some searches about the topic and find out as much as you can then experiment.

So you make a public variable in which you reference the raw image

Next you define a list of images and make it public.

Then you load all your textures in the list using the editor.

In your code then you can reference the rawimage and then change its main texture to whatever you like from the list of images

could you make a sprite on screen (canvas, ui) with animations of each state (even one frame animation) and change the animation based on the type of weather using the animation controller?

As if there was just a “weather” that sits on the 2D…it’s “idle” animation is sunny, and it has a “stormy”, “snowy”, etc. When the weather changes, it changes its state and that forces an animation change.