Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Nairda_Draziw · May 24, 2015 at 02:16 PM · prefabscene

Passing array of prefabs to next scene

I have two scenes. In one scene there are villages that have Level script attached. This script contains information about the level and most importantly prefabs of monsters that should be spawned for this level.

alt text

When a village is clicked it loads a second scene in which it should spawn monsters that were assigned to the village in the Level script. I don't know what is the best way to pass these prefabs from the village to the next scene to, where spawner will use them. What I did is:

I have a static variable, which is a reference to the Level script from the selected village.

 public class ApplicationModel
 {
     public static Level selectedLevel = null;
 }  

   

When village is clicked it assigns the Level script to the static variable.

 public class Level : MonoBehaviour 
 {
     public GameObject[] easy;
     public GameObject[] medium;  
     public GameObject[] hard;  
     public GameObject boss;  
     
     public int totalUnits;                       
     public float spawnTime;                      
     
     void OnMouseDown() 
     {
         ApplicationModel.selectedLevel = this;
 
         Application.LoadLevel("forest");
     }
 }

When a forest scene is loaded, in a different script I retrieve information and prefabs from the static selectedLevel variable

 public class LevelSceneManager : MonoBehaviour 
 {
     public EnemySpawner spawner;
 
     void Start () 
     {
         if(spawner != null && ApplicationModel.selectedLevel != null)
         {
             spawner.easy = ApplicationModel.selectedLevel.easy;
             spawner.medium = ApplicationModel.selectedLevel.easy;
             spawner.hard = ApplicationModel.selectedLevel.hard;
             spawner.boss = ApplicationModel.selectedLevel.boss;
             spawner.spawnTime = ApplicationModel.selectedLevel.spawnTime;
             spawner.totalUnits = ApplicationModel.selectedLevel.totalUnits;
         }
     }
 }

The problem is that ApplicationModel.selectedLevel is null, which might be because the GameObject of that script was destroyed, but still I can access its variables e.g. ApplicationModel.selectedLevel.easy is not null. While this approach works fine if I will remove if(ApplicationModel.selectedLevel != null) statement, I'm concerned about memory leaks that it might cause. When you will go back to the world scene, all arrays from the previously selectedLevel e.g. easy are still alive. What if you click a different village, will it destroy these arrays from the previous selectedLevel or will they be kept somewhere in the memory? Is there any better solution to pass prefabs assigned to a script in the editor from one scene and access them in the other scene?

level-script.jpg (16.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by redeemer · May 24, 2015 at 11:44 PM

Maybe this ould help you. Why don't you try to mantain an object with the script where the info is?

You can do this just by using DontDestroyOnLoad() method. Just add to the Start or Awake method :

DontDestroyOnLoad(this.gameObject);

That will maintain the object passed as argument between scenes.

I leave you the link to the docs : http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Comment
Add comment · Show 5 · 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 Nairda_Draziw · May 25, 2015 at 08:23 AM 0
Share

I shouldn't use DontDestroyOnLoad in Awake or Start method, because there are around 60 level objects in the scene, all of them would remain, while only 1 is needed. I guess I could call this method before loading a "forest" scene, but the question is will this object still remain even after loading back to the "world" scene from the "forest" scene?

avatar image Lo0NuhtiK · May 25, 2015 at 08:39 AM 0
Share

@Nairda_Draziw :

I shouldn't use DontDestroyOnLoad in Awake or Start method, because there are around 60 level objects in the scene, all of them would remain, while only 1 is needed.

     public static GameObject the$$anonymous$$eeper ;
 
     void Awake()
     {
         if(!the$$anonymous$$eeper) the$$anonymous$$eeper = this.gameObject ;
 
         DontDestroyOnLoad(the$$anonymous$$eeper) ;
     }
avatar image redeemer · May 25, 2015 at 10:11 AM 0
Share

There are a lot of ways to only call that method in the object you want, one of those is the above, as Lo0Nuhti$$anonymous$$ posted. Other options would be to do a script just for that object, or to invoke it depending on the tag... something like :

      void Awake()
      {
          if(this.CompareTag == "ObjectTag")       DontDestroyOnLoad(this.gameObject);
      }
avatar image Nairda_Draziw · May 25, 2015 at 10:35 AM 0
Share

Will this object remain if I will load another scene? Let's say I call DontDestroyOnLoad method then I load the forest scene. Being in the forest scene I load another scene. Will this object still remain or will it be destroyed?

avatar image redeemer · May 25, 2015 at 10:52 AM 0
Share

Yes, it will remain through all scene loads until you destroy it manually

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prefab is completly messed up 2 Answers

Passing a prefab between scenes? 1 Answer

Prefab looks different in different scenes 0 Answers

How to save all scene? 1 Answer

How do I select a prefab that was just instantiated from an editor window? 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