Skip to content

A few scripts that help Unity objects remember links to prefabs between Edit and Play mode

License

Notifications You must be signed in to change notification settings

skylord-a52/Unity-Remember-Self-Prefab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Unity-Remember-Self-Prefab

In Unity, GameObjects derived from prefabs don't inherently keep a reference to those prefabs. Furthermore, to prevent serialization issues, when a prefab with a reference to itself is instantiated, the new object gets a reference to itself -- not the original prefab.

This repository provides a few scripts that allow developers to easily create and maintain stable references to source prefabs.

How to use:

Copy RememberPrefab.cs to your scripts folder and RememberPrefabCustomEditor.cs to a folder named Editor.

Add RememberPrefab as a component to the desired object and select the appropriate prefab reference in the inspector. Note: Due to limitations on how Unity loads files in runtime, RememberPrefab can only store references to prefabs inside of a folder named Resources. This folder can be anywhere, there can be multiple such folders, and it can have its own subfolders. But there needs to be a folder named "Resources" somewhere in the path for this component to work. See https://docs.unity3d.com/ScriptReference/Resources.html for more information.

image

Call GetComponent<RememberPrefab>().Prefab to get a reference to the prefab in code -- this works in either Edit or Play mode, and the reference always points to the same object no matter what.

Notes

Not guaranteed to work correctly when values of RememberPrefab are modified via script instead of the inspector, particularly during builds.

About

A few scripts that help Unity objects remember links to prefabs between Edit and Play mode

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages