Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_5MNMGdWPwzVWig · Mar 29, 2020 at 07:06 PM · imageienumeratorstarturlfunction call

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Mar 30, 2020 at 05:42 PM

@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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by unity_5MNMGdWPwzVWig · Mar 30, 2020 at 11:36 PM

@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.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image streeetwalker · Mar 31, 2020 at 05:43 AM 0
Share

Ah yes, well, I shouldn't have said, "$$anonymous$$ost often as Singleton" - your reply clarifies the logic for me - if you use DontDestroyOnLoad with class attached to the object with set up as Singleton, you won't get another copy of the object.

Well, if it's any consolation, I can't think of how many times I have to redo because of choices I made based on lack of knowledge - pretty much all the time. Really, its the way to learn ;-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flash Image once 1 Answer

Can you pass a RawImage to an IEnumerator in another script? 0 Answers

Share screenshot on FB without SDK? 1 Answer

How can i WaitForSeconds after destroying GameObject to load the next Scene? 0 Answers

How do you create a generic function for custom types that are inherited from a generic class? C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges