Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 adrian-taylor09 · Jul 08, 2019 at 05:17 AM · referencehierarchyscriptable object

Create reference to Hierarchy objects that persists between different scene loads

Hi there, I need some help solving an issue:

I have created a basic custom occlusion culling system. The system creates a list of visible objects (as in a list of mesh renderers) for every camera position in my game. At the moment It creates this list in the editor as it is rather time consuming and I don't want to do this in the game. When I run the game on a device, I would like to turn on/off the mesh renderers based on whether or not they are in the "baked" list.

The problem I have is how do I store a list of MeshRenderers that live in the hierarchy in a scriptable object or a file of some sort so that they will persist between scene loads? I tried looking into GUID but it seems that this is for assets in the asset database rather than objects in the hierarchy. I also looked into instance ID, but it seems that these ID's are not the same everytime you load the scene.

What is the correct way to get a reference to a GameObject in the hierarchy that won't change when you reload the scene?

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 misher · Jul 08, 2019 at 07:40 AM 0
Share

You can't reference assets (within you project view) in your instantiated objects (in scene). You still can come up with some dynamic look up solution during runtime but it is somewhat weird. I would instantiate all objects from a list of prepared prefabs at the beginning of the game rather.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Jul 08, 2019 at 07:12 AM

You can make use of the DontDestroyOnLoad() function. If you call this function in the Start() function of your object, it won't be destroyed when the next scene is loaded. If you save the List of Renderers in this script and make the renderer gameobjects childs of this object, they will all be transferred to the next scene. This will probably be a good way to handle the situation.

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 adrian-taylor09 · Jul 08, 2019 at 07:16 AM 0
Share

Thanks for the suggestion. Although I actually want to create the list of objects in the editor and have them available in the game. So when I start the game I need to already have a list of objects ready to go

avatar image
0

Answer by misher · Jul 08, 2019 at 07:37 AM

In your scene you have instances of assets and they are organized into hierarchy of gameobjects. An instance in a scene (basically, components attached to game objects) can naturally reference your assets (in project view) but not the other way around, there is no way an asset can reference an instance in the scene.

You can create a list of prefabs to be instantiated and make them into list (in some scriptable object for example), then, when your game starts you can instantiate them all in the scene. Or you can already have all instances in the then and then look up by name or some other attribute from your list of prefabs, note that you can't use PrefabUtility.GetPrefabParent(gameObject)); during runtime to look up for the original prefab of instatiated object, you can do this only in Editor.

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

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

113 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

Related Questions

Reference scriptable object by path 0 Answers

how to stop tag switching on same object? 1 Answer

Scriptable object saves but what about objects it references? 0 Answers

Particle System playing spontaneously 1 Answer

How do you save a reference to a prefab's script in a ScriptableObject? 0 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