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 /
This question was closed May 01, 2017 at 07:38 AM by AkshayCR718 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AkshayCR718 · Mar 19, 2017 at 05:08 PM · uiimagevariablesui imageavatar

Get Image from another scene

I am making a sort of text based game. In the main menu of the game, you get to select an avatar. This whole avatar system does not run on databases or lists. Its kind of setactive this image when int = some number. and hides the rest. I also created a bool for every image. It becomes true when an particular image is clicked and becomes false when another image is active. I have another scene named main game. I want the selected avatar from the main menu scene to main game scene.

Here's the script of the avatar selection which is on the main menu.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class avatarselection : MonoBehaviour {
 
     public int numberOfItems =1;
     public GameObject image1go, image2go, image3go, image4go, image5go;
 
 static public bool img1;
     static public bool img2;
     static public bool img3;
     static public bool img4;
     static public bool img5;
 
 void Start () 
     {
         image1go.SetActive (false);
         image2go.SetActive (false);
         image3go.SetActive (false);
         image4go.SetActive (false);
         image5go.SetActive (false);
         //defaulImage.SetActive (false);
     }
 
 void Update () 
     {
         
 
 
 
         if (numberOfItems == 1) 
         {
             //defaulImage.SetActive (false);
             image1go.SetActive (true);
             image2go.SetActive (false);
             image3go.SetActive (false);
             image4go.SetActive (false);
             image5go.SetActive (false);
             img1 = true;
             img2 = false;
             img3 = false;
             img4 = false;
             img5 = false;
         }
         if (numberOfItems == 2) 
         {
             //defaulImage.SetActive (false);
             image1go.SetActive (false);
             image2go.SetActive (true);
             image3go.SetActive (false);
             image4go.SetActive (false);
             image5go.SetActive (false);
             img1 = false;
             img2 = true;
             img3 = false;
             img4 = false;
             img5 = false;
         }
         if (numberOfItems == 3) 
         {
             //defaulImage.SetActive (false);
             image1go.SetActive (false);
             image2go.SetActive (false);
             image3go.SetActive (true);
             image4go.SetActive (false);
             image5go.SetActive (false);
             img1 = false;
             img2 = false;
             img3 = true;
             img4 = false;
             img5 = false;
         }
         if (numberOfItems == 4) 
         {
             //defaulImage.SetActive (false);
             image1go.SetActive (false);
             image2go.SetActive (false);
             image3go.SetActive (false);
             image4go.SetActive (true);
             image5go.SetActive (false);
             img1 = false;
             img2 = false;
             img3 = false;
             img4 = true;
             img5 = false;
         }
         if (numberOfItems == 5) 
         {
             //defaulImage.SetActive (false);
             image1go.SetActive (false);
             image2go.SetActive (false);
             image3go.SetActive (false);
             image4go.SetActive (false);
             image5go.SetActive (true);
             img1 = false;
             img2 = false;
             img3 = false;
             img4 = false;
             img5 = true;
         }
             
     }
 
     public void rightSelection()
     {
         numberOfItems += 1;
     }
 
     public void leftSelection()
     {
         numberOfItems -= 1;
     }
 }
 
 

Now this is the avatarShow script in the scene main game. This gets the bool value from script above and enables image according to the bool.

 using UnityEngine;
 using System.Collections;
 
 public class avatarshow : MonoBehaviour {
 
     public GameObject image1, image2, image3, image4, image5;
 
 
 
     // Use this for initialization
     void Start () 
     {
         image1.SetActive (false);
         image2.SetActive (false);
         image3.SetActive (false);
         image4.SetActive (false);
         image5.SetActive (false);
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (avatarselection.img1 == true) 
         {
             image1.SetActive (true);
         }
         if (avatarselection.img2 == true) 
         {
             image2.SetActive (true);
         }
         if (avatarselection.img3 == true) 
         {
             image3.SetActive (true);
         }
         if (avatarselection.img4 == true) 
         {
             image4.SetActive (true);
         }
         if (avatarselection.img5 == true) 
         {
             image5.SetActive (true);
         }
     }
 }

But this does not work. Plz Help.

avatar-selection.txt (2.4 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by RobAnthem · Mar 20, 2017 at 01:46 AM

First of all, you are in desperate need of some loops and overall optimizations.

Also, static members on monobehaviours are still set to null when the monobehaviour is destroyed, IE, new scene loaded. So at the bottom is a class to store the info in. Also there is a lot better ways of doing this, but hopefully this points you in the right direction. Dynamically loading the image or texture is a good example of a better way.

This should be MUCH cleaner than your current code.

  public class avatarselection : MonoBehaviour
 {
     public int NumberOfItems
     {
         get { return numberOfItems; }
         set { if (value != numberOfItems)
             {
                 doSwitch = true;
             }
             numberOfItems = value;
             numberOfItems = Mathf.Clamp(numberOfItems, 0, images.Length);
             GameManager.avatarSelection = numberOfItems;
         }
     }
     private int numberOfItems;
     public GameObject[] images;
     public bool doSwitch;
     void Start()
     {
         for (int i = 0; i < images.Length; i++)
         {
             images[i].SetActive(false);
         }
         //defaulImage.SetActive (false);
     }
 
     void Update()
     {
         if (doSwitch)
         {
             for (int i = 0; i < images.Length; i++)
             {
                 if (i == NumberOfItems)
                 {
                     images[i].SetActive(true);
                 }
                 else
                 {
                     images[i].SetActive(false);
                 }
             }
             doSwitch = false;
         }
     }
     public void setSelection(bool direction)
     {
         if (direction)
         {
             NumberOfItems += 1;
         }
         else
         {
             NumberOfItems -= 1;
         }
     }
 }
 

Now for your avatar class.

 public class avatarshow : MonoBehaviour
 {
     public GameObject[] images;
     void Start()
     {
         for (int i = 0; i < images.Length; i++)
         {
             if (GameManager.avatarSelection == i)
             {
                 images[i].SetActive(true);
             }
             else
             {
                 images[i].SetActive(false);
             }
         }
     }
 }


And finally where we ACTUALLY keep our reference

 public static class GameManager
 {
     public static int avatarSelection;
 }
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 Spidlee · Mar 21, 2017 at 01:49 AM

To address retaining states from scene to scene, I would usually handle it via the singleton approach, where I have a gameobject that remains "Alive" between the scenes.

So the concept is that you'd make a public static instance of the object, and apply DontDestroyOnLoad to it. That way, the object remains Alive when the scene changes, and thus retains the necessary values you need carried over.

One thing to be aware of is, if you have references to objects in the previous scenes, those would become null as they got destroyed when the scene changed.

This is a good tutorial reference: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager

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

7 People are following this question.

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

Related Questions

How to move UI Image under the other UI elements? 3 Answers

converting Textures to Images ? 1 Answer

Images getting distorted -SOLVED 2 Answers

Show only specific part of UI.Image 0 Answers

Make images fit in UI 0 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