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 /
This question was closed Mar 14, 2019 at 09:34 PM by Colthy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Colthy · Mar 14, 2019 at 04:24 AM · sceneimagescene-loading

Looking for a better way of dynamically loading an image between scenes

Hey guys, for a couple hours now I've been trying to create a script that allows me to do the following things:


  • Dynamically copy one GameObject's sprite to another (hidden) GameObject's sprite

  • Load the GameObject with the dynamically loaded sprite to another scene

  • Do all of this without having the GameObject with the dynamically changing sprite visible until the scene changes to the appropriate one.


I want to do all of this because I'm creating a player avatar (read: like a forum avatar) selection menu where the player chooses one from a number of different sprites. After the player has selected a sprite, a placeholder sprite in the middle of the screen changes to represent the decision made by the player. Because that sprite is now considered the player's avatar, I want it to be visible on other scenes as well, but I can't just DontDestroyOnLoad the placeholder sprite, because it also serves the purpose of a button. Transferring the whole button seems like a bad idea.


I played around with SetActive, but couldn't make it work. Somehow, I got the idea that a transparent image, which only regains its alpha on scene change to the appropriate scene is a smart way to deal with my issue. And, to my surprise, it actually worked.


The code in Awake() deals with the problem of having a child GameObject that you want to use DontDestroyOnLoad on.


The code I used to accomplish the task is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class AvatarPersistence : MonoBehaviour
 {
     public GameObject PlayerAvatar;
     public string SceneToBeActive; //Assign in inspector
     private Image img_PlayerAvatar;
     private Image new_PlayerAvatar;
     
     void Start()
     {
         img_PlayerAvatar = PlayerAvatar.GetComponent<Image>();
         new_PlayerAvatar = this.GetComponent<Image>();
     }
 
     void Update()
     {
         new_PlayerAvatar.sprite = img_PlayerAvatar.sprite;
         if (SceneManager.GetActiveScene().name == SceneToBeActive)
         {
             var tempColor = new_PlayerAvatar.color;
             tempColor.a = 1f; 
             new_PlayerAvatar.color = tempColor;//Makes transparent Player Avatar visible again on Assigned Scene change
         }
     }
 
     void Awake()
     {
         Transform parentTransform = this.transform;
 
         while (parentTransform.parent != null)
         {
             parentTransform = parentTransform.parent;
         }
         DontDestroyOnLoad(parentTransform.gameObject);
     }
 }



My question is, is there a more elegant way of accomplishing the tasks I previously outlined? It seems to me that playing around with the alpha of an image is not the best way of doing this.


Thanks, B.

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Colthy · Mar 14, 2019 at 06:26 PM

Hey again, I managed to do it by editing the enabled/disabled state of the Image component. It feels like it's way better than my previous one. Here's the code:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class PlayerAvatarHandler : MonoBehaviour
 {
     public GameObject PlayerAvatar;
     public string SceneToBeActive;
     public string SceneToUpdateIn;
     static bool imageNotChanged = true;
 
     void Update()
     {
         if (SceneManager.GetActiveScene().name == SceneToUpdateIn)
         {
             this.GetComponent<Image>().sprite = PlayerAvatar.GetComponent<Image>().sprite;
         }
 
         if ((SceneManager.GetActiveScene().name == SceneToBeActive) && imageNotChanged)
         {
             this.GetComponent<Image>().enabled = true;
             imageNotChanged = false;
         }
 
     }
 
     void Awake()
     {
         Transform parentTransform = this.transform;
 
         while (parentTransform.parent != null)
         {
             parentTransform = parentTransform.parent;
         }
         DontDestroyOnLoad(parentTransform.gameObject);
     }
 }

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

Follow this Question

Answers Answers and Comments

188 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

Related Questions

How do you call a function once 2 Answers

My Timeline doesn't works how should do it 0 Answers

Cannot interact with new scene 0 Answers

using Scenemanager 1 Answer

How do i create a scene variable? 3 Answers


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