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!