Script to change Source Image

Hello,

I’m trying to edit the sprite of a panel like this:
public void Show()
{
GameObject.Find(“RightPanel”).GetComponent().sprite = Resources.Load(“eclipse”);
}

It’s called from a button.
Thie “Source Image” change from “Background” to “None” but doesn’t get the sprite.
The “eclipse” sprite works when I drag it in the “Source Image”.

Finally I don’t get any error.

Thank you for your help.

So I worked a bit on the code to this point:

public class LaunchNotePad : MonoBehaviour
{
public string processId = string.Empty
public GameObject rightPanel = null;

private static readonly HttpClient client = new HttpClient();
public void Launch()
{
CreateItem item = new CreateItem()
{
IsTopMost = true,
ProcessPath = “notepad”,
ProcessURL = “file:///C:/Windows/notepad.exe”,
Argument = “”,
ScreenNumber = 1,
Fullscreen = false,
Title = “NotePad”
};

string data = JsonConvert.SerializeObject(item);

var buffer = System.Text.Encoding.UTF8.GetBytes(data);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue(“application/json”);

HttpResponseMessage response = client.PostAsync(“http://localhost:5000/api/create/”, byteContent).Result;

processId = response.Content.ReadAsStringAsync().Result;

GameObject sphere = GameObject.Find(“Sphere”);
sphere.SetActive(false);
rightPanel = GameObject.Find(“RightPanel2”);
}

void Update()
{
if (!rightPanel)
return;
HttpResponseMessage response = client.GetAsync(“http://localhost:5000/api/snapshot/” + processId).Result;

var imgName = response.Content.ReadAsStringAsync().Result;

Texture2D tex = null;
byte[ ] fileData;

if (File.Exists(imgName))
{
fileData = File.ReadAllBytes(imgName);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //…this will auto-resize the texture dimensions.
}

RectTransform rectTransform = rightPanel.GetComponent();
rightPanel.GetComponent<UnityEngine.UI.Image>().sprite = Sprite.Create(tex, rectTransform.rect, new Vector2(1, 1));
}
}

Because of the Update I generate regular screenshot and I want them to be used a sprite for the “RightPanel2”.

Even if the GameObject seems to be reached, the sprite is never showed.
In the Editor, the Source Image go from "None (Sprite) to " ".
What do I miss do you think?

So it actually works.
It’s just that the image Color was set to 0,0,0,0 which means transparent. -_-
Yeah!