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
2
Question by thorbrian · Nov 21, 2011 at 08:15 PM · gameobjectlightmapscriptableobject

How to reference a scene gameobject from a custom project asset?

Does anybody have any method for being able to have a serialized reference to a game object in a scene in a custom asset in the project?


To explain what I mean - I've written a tool for saving, loading & merging lightmap bakes to a custom asset in the project tab (i.e. something that derives from ScriptableObject, was created with ScriptableObject.CreateInstance and saved with AssetDatabase.CreateAsset). The one problem with it is that all the ways I could find to store which GameObject has what lightmap settings fails to keep track of the gameobject in interesting scenes over time.

One method I tried was saving the path to the gameobject in the scene's hierarchy tab as a string - that works terrible because the paths are not unique, and because when the hierarchy is reordered the paths change, even though the game objects are the same

A second method was GameObject.GetInstanceID and EditorUtility.InstanceIDToObject - this fails because the instance ID's change every time the scene is reloaded, so EditorUtility.InstanceIDToObject returns null after closing/opening or changing scenes

A third method is actually using a GameObject reference in the custom asset - which seems like it is the right way and seems like it ought to work great (can browse the properties of the custom asset and see the gameobjects are all set in the property inspector), and it does work for a while - unfortunately the moment any scene other the one containing the GameObject is loaded, the saved references are all killed in the asset, so that AssetDatabase.LoadAssetAtPath returns an object with all gameobject references set to null :(


here's some code bits that may help place what I'm talking about:

 // got this class in "SavedLightmapData.cs"
 public class SavedLightmapData : ScriptableObject 
 {
     [System.Serializable]
     public class LightmapObjectAtlasPair
     {
         [SerializeField]
         public GameObject gameObject;  // this is what goes null
         public AtlasPosition atlasPosition;
 
         public LightmapObjectAtlasPair(GameObject go, AtlasPosition pos)
         {
             gameObject = go;
             atlasPosition = pos;
         }
     };
 
     [System.Serializable]
     public class LightmapCopiedData
     {
         public int activeLightmapIndex;
         public Texture2D farMap;
         public Texture2D nearMap;
         public LightmapObjectAtlasPair[] atlasPairs;
     }
 
     public LightmapHelpers.LightmapCopiedData[] lightmapData;
 }
 
 // and the code to save one of these looks like this:
 SavedLightmapData savedData = ScriptableObject.CreateInstance<SavedLightmapData>();
 savedData.lightmapData = data;
 AssetDatabase.CreateAsset(savedData, outputName);
 
 // and the code to load looks like this:
 SavedLightmapData savedData = (SavedLightmapData)AssetDatabase.LoadAssetAtPath(inputName, typeof(SavedLightmapData));
Comment
Add comment · Show 3
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 chuckrussell · Apr 16, 2012 at 06:24 PM 0
Share

Have you found a solution to this problem? I am also having that problem.

avatar image thorbrian · Apr 17, 2012 at 07:33 AM 0
Share

@chuckrussell - I haven't found a solution that allows me to store gameObject references outside of the scene, but a workaround is to store the gameObject references inside an object in the scene itself.

avatar image meat5000 ♦ · Jun 16, 2014 at 12:51 PM 0
Share

-1 Answers Bug occurred here. $$anonymous$$ods, please read the QA

[2]: http://answers.unity3d.com/questions/526240/meta-moderation-page-1-answers-bug.html

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Radivarig · Nov 29, 2014 at 02:41 PM

Documentation says: "assets can't store references to objects in a scene."

I'd go with hidden child, then reference to parent.

 string uniqueID = "generateUniqueID";
 GameObject child = new GameObject(uniqueID);
 child.transform.parent = targetedSceneObject;
 child.hideFlags = HideFlags.HideInHierarchy;

and fetch like this GameObject.Find(uniqueID).transform.parent;

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 DaveA · Nov 21, 2011 at 08:54 PM

" the paths are not unique, " - What I did was make an Editor script to give unique names to all the objects. Not as pretty, but at least you can always find the uniquely-named object.

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 thorbrian · Nov 21, 2011 at 09:42 PM 0
Share

$$anonymous$$y duplicate named objects are usually actually part of a model - rena$$anonymous$$g them doesn't work great. Also, it doesn't solve changing the hierarchy :(

avatar image
0

Answer by Adrian · Jan 27, 2012 at 12:15 PM

I don't think Unity allows references from assets to scenes, only the other way round.

An option to work around this limitation could be storing a "entry point" game object in the scene with an unique name, so that you can easily locate it in every scene using GameObject.Find(). Then add a script that contains references to the game objects in the scene and reference them from your assets using either the array index or a unique identifier. Optionally you can also hide that object using HideFlags so it doesn't clutter the hierarchy view.

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 meat5000 ♦ · Jun 16, 2014 at 12:52 PM 0
Share

Posted by @tswalk; lost to the ether.

I believe Adrian has the right direction, as I too have been trying to get gameobject references in a scene saved to an asset (assetdatabase) with serializable / scriptableobject class and it just doesn't seem to work at all. so, how unity is keeping track of object in hierarchy (either prefab'd or not) and its' unique id system out of our hands is way beyond understanding. there just is no reason why we have to come up with half-cocked solution for this at all.

avatar image tswalk · Jun 18, 2014 at 04:15 PM 0
Share

I believe Adrian has the right direction, as I too have been trying to get gameobject references in a scene saved to an asset (assetdatabase) with serializable / scriptableobject class and it just doesn't seem to work at all.

so, how unity is keeping track of object in hierarchy (either prefab'd or not) and its' unique id system out of our hands is way beyond understanding.

there just is no reason why we have to come up with half-cocked solution for this at all.

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

9 People are following this question.

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

Related Questions

Get a parent GameObject from a class on a script attached to a prefab,Getting a parent Gameobject from a component on a script attached to a prefab 1 Answer

Version Control and Unity Assets 0 Answers

Copy gameObject from one prefab to another using ScriptableWizard 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Instantiate with random scriptableObject 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