Load a custom image by calling from another function

Basically I have a LevelChanger script that loads a new scene, but I want based on what object I clicked in the previous scene (they both trigger the new scene), to load a custom image from an url into the new scene. I sorted most of it out but I can’t seem to make the right image load.

Let’s say for the sake of the experiment that I have 2 gameobjects, obj1 and obj2. When I click obj1, along with the loading of the new scene I will be calling in my LevelChanger script the function LIMG.ChooseIMG(1) from my other LoadIMG script (I defined it in the LevelChanger script like this: public LoadIMG LIMG; ). If I clock obj2, I will be calling the function LIMG.ChooseIMG(2).

This is the code that I have in the LoadIMG script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoadIMG : MonoBehaviour
{
    public string url1 = "https://www.solidbackgrounds.com/images/3840x2160/3840x2160-dark-red-solid-color-background.jpg";
    public string url2 = "https://www.masala.com/public/images/2019/06/21/57554.jpg";
    public RawImage Img;
    public int n;

    void Awake()
    {
        Img = this.gameObject.GetComponent<RawImage>();

    }

    public void ChooseImage(int i)
    {
        if (i == 1)
        {
            Debug.Log("Entered the ChooseImage function - if clause no. 1");
            n = 1;
           // url = "https://www.solidbackgrounds.com/images/3840x2160/3840x2160-dark-red-solid-color-background.jpg";
        }
        else if (i == 2)
        {
            n = 2;
           // url = "https://www.masala.com/public/images/2019/06/21/57554.jpg";
        }
    }

    [System.Obsolete]
    IEnumerator Start()
    {

        Debug.Log("Loading...");

        if (n == 1)
        {
            WWW wwwLoader = new WWW(url1);
            yield return wwwLoader;
            Debug.Log("Entered the IEnumerator Start");

            Img.texture = wwwLoader.texture;
        }
        else if (n == 2)
        {
            WWW wwwLoader = new WWW(url2);
            yield return wwwLoader;

            Img.texture = wwwLoader.texture;
        }
        Debug.Log("n = " + n);
        Debug.Log("Loaded");
        


    }
   
}

With the debug testing that I have done, I found out that the function gets called correctly, but n does not change its value, instead it remains 0, meaning that no image will load in the end… My guess is that I am using wrong the IEnumerator function but I have no idea how else I could go around this.
Any help would be appreciated, thank you in advance!

@unity_5MNMGdWPwzVWig , Did you get this figured out? I don’t know I this is part of the issue you’re facing, but to my knowledge everything is destroyed when you load a new scene - including coroutines.

It’s not clear in your description what the sequence of events with respect to the above script.

You click one of two buttons, and call ChooseImage with an index (i) in order to set n for your co routine? That is before you statement that loads the new scene?

However in the script above Start will run as soon as that script is instantiated. So at the time Start runs, yes, n = 0;

It seems like you want the coroutine to run after the new scene loads? If you want to pass a value to a new scene, you have to use some script on an object that is set to DontDestroyOnLoad (most often set up as Singleton to act as " global variable repository"), or use a static class to share values between scenes.

@streeetwalker
Thank you for your response!

I gave up on the idea because I didn’t know what to do… but I am keen on trying again with the information you told me. I made the very stupid choice of making different scenes and every time I click a different object I will load a different scene that has a different image, instead of just changing the image in the same scene.

You click one of two buttons, and call
ChooseImage with an index (i) in order
to set n for your co routine? That is
before you statement that loads the
new scene?
^^This is correct. I click on an object that triggers both the loading of another scene, and the ChooseImage function with the ‘i’ index that changes the n variable used in the co routine. I tried both putting it before and after the statement that loads the new scene.
Defining ‘n’ as a static int should be a good idea and I will definitely try that out, however I don’t really know how to work around the whole “DontDestroyOnLoad” thingie, as I tried it before with something else and it just ended up duplicating my gameobject over and over whenever I changed between scenes.