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 JoeW97 · Nov 22, 2013 at 06:15 AM · gameobjecteditorreference

Objects created in Editor script lose their reference after Play/Stop?

My C# editor class (the target script) declares a public GameObject, which is instantiated in the initial Reset() function via:

 folder = new GameObject("My Folder");

But after going in to Play mode and stopping again, folder now has a null reference. It (and its contents) are still in the Hierarchy, but I can't access them without a reference. I'm quite new to C#, coming from C++, so I'm still a bit fuzzy on allocation and persistence.

Is the problem because the script is [ExecuteInEditMode] ?

What do I need to do to keep a reference to 'folder'?

Thanks

Comment
Add comment · Show 1
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 iwaldrop · Nov 22, 2013 at 07:06 AM 0
Share

AFAI$$anonymous$$ editor scripts aren't executed in edit mode. If you need an update then just attach a delegate to the EditorApplication.update callback. There are others as well and a list of them is here.

1 Reply

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

Answer by iwaldrop · Nov 22, 2013 at 07:04 AM

This is expected behavior for editor scripts. You should do this:

 const string FOLDER_NAME = "My Folder";
 
 private GameObject folder;
 
 void Awake()
 {
     folder = GameObject.Find(FOLDER_NAME) ?? new GameObject(FOLDER_NAME);
 }
Comment
Add comment · Show 3 · 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 JoeW97 · Nov 22, 2013 at 07:11 AM 0
Share

Thanks, that's certainly going to be useful for ensuring certain objects exist or are recreated. In this case though, maybe it won't work because by simply re-making the folder I'll lose all the contents ( a bunch of prefabs). So, does this mean the expected method should be to simply rebuild EVERYTHING each time?

avatar image iwaldrop · Nov 22, 2013 at 07:17 AM 0
Share

You shouldn't lose anything in the GameObject if the scene is saved. But if you do (I can't think of every corner case AT$$anonymous$$), then rebuilding the hierarchy is a trivial matter. By augmenting the above with the following, you can create as many prefabs as you want under your folder whenever it is rebuilt. Just drag those prefabs into the inspector (assu$$anonymous$$g you have one).

 public GameObject[] folderContents;
 
 void Awake()
 {
     bool needsRebuild = folder == null;
     // find or create folder here
     if (needsRebuild)
     {
         foreach (GameObject go in folderContents)
         {
             GameObject instance = GameObject.Instantiate(go, Vector3.zero, Quaternion.identity);
             instance.transform.parent = folder.transform;
         }
     }
 }

If you don't have an inspector, then look into Resources.LoadAssetAtPath

EDIT: $$anonymous$$odified code so it should execute.

avatar image JoeW97 · Nov 22, 2013 at 08:03 AM 0
Share

:) It's always a tricky decision as to how much detail to omit when first asking a question - I left out details I thought were irrelevant, but now I can see are highly relevant. So, first, thanks for a great explanation!, although this isn't a complete solution, at least now I understand what needs to be done.

$$anonymous$$y Editor's functionality is to create lots of procedural geometry, with realtime parametric control, and put it in a folder in the hierarchy. Of course, after entering/leaving Play, the contents of that folder can no longer be parametrically edited, because all the references have been lost. So after reading your explanation, it seems the best solution might be to destroy the originals in the hierarchy, and rebuild everything, thereby re-establishing parametric control over all the objects.

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

17 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

Related Questions

Is there anyway to batch renaming via Editor [not on Runtime] multiple game objects in the hierarchy ? 4 Answers

Accessing other GameObjects Script Variables 2 Answers

OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers

The name `gameObject' does not exist...? Yes i did my research. 1 Answer

Finding multiple objects by the same tag as to place them in one array? 2 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