Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mixer468 · May 31, 2021 at 10:52 AM · editorlistgenerationarchitecturepersistence

How do i go about generating stuff in editor and keeping it in play mode

So, i'm generating the track using segments and attaching them to the end of the track. I created a custom editor script to add "generate" button to the inspector of my track object. In editor it works fine, but as soon as i hit play, my list of segments is empty, while scene still have those segment objects as children of track. I tried to assign some assets to segments in the editor and it keeps them. So i guess i'm doing something wrong with serialization considering unity only serializes values if they are set in inspector.

List is defined like that:

 [SerializeField]
 public List<GameObject> segments; 


And i add segments with that:

 void addRandomSegmentsToTrack(int segmentsToAdd)
 {
     initializeIfNoSegmentsPresent();
     foreach (var segment in Range(0, segmentsToAdd))
     {     
         segments.Add(Instantiate(segmentPrefab, trackEndPoint, trackEndRotation));
         getSegmentScript(segments.Last()).constructSegment();
         segments.Last().transform.parent = this.transform;
         updateTrackEndPointAndRotation();
     }             
 }
 
     void initializeIfNoSegmentsPresent()
     {        
         if(!segments.Any())
         {            
             trackEndPoint = transform.position;
             trackEndRotation = Quaternion.identity;
         }
     }

So i really puzzled how can i do something in editor and keep this changes in play mode?

I also have problem with unity events calling object methods in the editor. Maybe i'm making architectural mistakes by decentralizing everything and just passing references to scene components in inspector?

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
1
Best Answer

Answer by CodesCove · May 31, 2021 at 03:01 PM

Long story short: just set the data dirty after the changes or record object before making the changes. And to be sure it's saved save the scene automatically (optional)..

Preferred way (to support also undo)

 Undo.RecordObject(segments, "My undo add segment"); //this goes before making the change
 optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

or like this (this will not record the change)

 EditorUtility.SetDirty(segments); //this goes after the changes
 optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

or set the whole scene dirty (this will not record the changes)

 EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); //this goes after the changes
 optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

Notice! Save the scene only once when you have iterated to whole list..

hope you get it working!

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 mixer468 · Jun 01, 2021 at 06:42 AM 0
Share

Hey, thanks a lot! Setting

 Undo.RecordObject(this, "Undo on generation");
 EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

before proceeding to clear the track and generating new segments works. Now it persists between edit and play mode. Although undo button doesn't change much (only gives me a glitch on regeneration when not all segments are deleted from previous one). Also it seems like a bit of a hack, recording changes for undo only to store them in game object.

avatar image
0

Answer by SadeqSoli · May 31, 2021 at 02:36 PM

Hi @mixer468 , I didn't understand why you want to do something in editor first then go to the runtime??? and I also didn't understand your problem with unity events... can you explain more please??? . BTW you can instantiate game objects at AWAKE Or START Method and put them as a child of a parent with a Horizontal, Vertical Or even Grid Layout group as you need them. Also there is the trick of pooling system to make sure it's not an expensive call on startup methods and don't give you any Calculating time, Anyway I hope I got your problem correctly and of course if you'd explain it better, I can help better I believe. good luck, cheers.

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 mixer468 · Jun 01, 2021 at 06:52 AM 0
Share

Hello, thanks for the answer. Although my question about persistent data already answered, i want to add a little. To elaborate more on the question, game objects generated by my script are left in the scene after play mode started, the problem is, all the data in attached scripts is empty, so i can't get info from segments which is generated on creation.

As for the architectural question, i really think i might be doing something wrong, but in my $$anonymous$$d i really want every game object be on it's own. As for example a camera cold be a child of a track object or a player object, but i have all kinds of play modes in $$anonymous$$d where camera should follow different objects, so i don't want to attach it, just reference scene objects in my camera script to follow accordingly to game state. And as far as implementing events in editor, i really think that it's logical to start work on camera placement in editor mode. For example, i fire an event on track generation, it would be convenient for camera to attach to the correct place of a track on this event, i could've quickly debug it just by visuals if it attaches incorrectly.

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

166 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

Related Questions

A node in a childnode? 1 Answer

How can i use 4.6 delegate lists in my scripts ? 1 Answer

How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers

Default values are not working in Editor 1 Answer

Why does Frame Selected toggle between position and bounds for procedural and imported meshes? 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