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 pborg · May 03, 2013 at 10:26 PM · gameobjectsaveloading

can gameobjects be stored as static?

The original problem involves real time strategy elements and saving objects in a scene before leaving. I need to be able to either save the state of the scene or save everything in the scene as is. My solution is to have a script called gamedatabase and basically override application.loadlevel() to a function within this script that saves everything before loading the level.

It only breaks when reloading the scene. Since the objects are static, I can't check them as I'm loading other scenes. It prints each name as it is stored so I know that is performing properly. I think the objects might be lost though as it leaves the scene if it is running on pass by reference. This is tied to an object so that the start function is called. Should I not be tying to anything? What would help this?

 static var built : GameObject[] = new GameObject[50];
 
 static var positions : Vector3[] = new Vector3[50];
 static var v : int = 0;
 function Start () 
 {
     print("gamedatabase start called");
     //if(EditorApplication.currentScene == gamedatabase.groundscene)
     //{
         print("loading objects");
         //destroy anything not terrain
         if(!gamedatabase.starting)
         {
             //var objects : GameObject[] = GameObject.FindObjectsOfType(typeof(MonoBehaviour)) as GameObject[]; 
             var objects : GameObject[] = GameObject.FindObjectsOfType(typeof(GameObject));
             for(var tobedestroyed : GameObject in objects)
             {
                 if(tobedestroyed.tag != "terrain" && tobedestroyed.tag != "MainCamera")
                 {
                     print("removing " + tobedestroyed.name);
                     //Destroy(tobedestroyed);
                 }    
             }
             for(var x : int = 0; x < v; x++)
             {
                 //add objects
                 //print("attempting to add " + built[x].name);
                 //--------------------------------it can't seem to add the objects; it says the objects have been destroyed
                 GameObject.Instantiate( built[x], positions[x], Quaternion.identity);
             }
         }
         gamedatabase.starting = false;
     //}
 }
 
 static function leavescene(toscene)
 {
       print("leave database called");
     print("current scene is " + EditorApplication.currentScene);
     //if(EditorApplication.currentScene == groundscene)
     //{
         var objects = GameObject.FindObjectsOfType(typeof(GameObject));
         v = 0;
         print("storing objects");
            var temp : GameObject;
         var temp2 : GameObject[] = new GameObject[50];
         for(var tobestored : GameObject in objects)
         {
             //print(tobestored.name);
             if(tobestored.tag != "terrain")
             {
                 built[v] = tobestored;
                    positions[v] = tobestored.transform.position;
                 print(tobestored.name + " stored");
                 v++;
             }    
         }
     //}
     Application.LoadLevel(toscene);
 }
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
1

Answer by iwaldrop · May 04, 2013 at 01:16 AM

Sure they can. But when you load a new scene your script probably has a new instance of itself created.

A good way to get around this is to actually not use a static method/variable, attach the 'persistance' script to a gameObject, and set that object not to be destroyed between scene loads.

 function Awake ()
 {
     DontDestroyOnLoad(transform.gameObject);
 }


Comment
Add comment · Show 2 · 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 pborg · May 04, 2013 at 11:12 PM 0
Share

I read the link, Does that mean that the objects from this scene will be in the next scene you load too? Oh and how to use a persistence script?

avatar image iwaldrop · May 05, 2013 at 03:07 AM 0
Share

By 'persistance script' I simply meant whatever script you use to call the above method. You use it like any other $$anonymous$$onoBehaviour. Say, for instance, you wanted a script that only kept a GO from being destroyed between scene loads. You might call it PreventDestructionBetweenScenes, and have only the Awake function as demonstrated above in it with the addition that it would remove itself so that it doesn't keep calling DontDestroyOnLoad every time you load a scene (because that would just be wasteful), like:

 function Awake ()
 {
     DontDestroyOnLoad(transform.gameObject);
     Destroy(this);
 }

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

13 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

Related Questions

save/load cloned game object 1 Answer

Problems with save 1 Answer

How to save/load references to components and prefabs 1 Answer

It's possible save materials and meshes into a prefab like the unity's model importer? 2 Answers

Save & Load Game question 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