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
3
Question by Jix · Nov 07, 2013 at 05:23 PM · editorprefabprefabutility

Need to make a reference in a prefab to itself not to the created instance

Hello,

Let's say that we have a prefab X and it has a script that has a reference called "prefab" to the prefab X, in other words it points to itself. I created the GameObject, attached my script on it, created a prefab, dragged the GameObject to it then selected the prefab and then dragged it to the reference "prefab" in the script.

My problem is when I drag the prefab into the scene the reference "prefab" refers to the created instance not the prefab X itself.

I understand that by doing the steps I did Unity thought that I want a reference in a GameObject referring to itself not to the actual prefab.

I tried the following to prevent instances of the prefab to set the value of "prefab" but it didn't work:

 GameObject prefab;
     public GameObject Prefab
     {
         set
         {
             PrefabType objectType = PrefabUtility.GetPrefabType(gameObject);
             if (objectType == PrefabType.Prefab)
                 prefab= value;
         }
         get
         {
             return prefab;
         }
     }

When I did that "prefab" became null, I thought that it will keep its value when it was still a prefab but apparently when an instance is created from prefab it's a new created GameObject with the same components then its values are copied from the prefab.

Any solution for that doesn't use Resources.Load()?

Comment
Add comment · Show 7
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 Tanshaydar · Nov 07, 2013 at 08:30 PM 0
Share

Do you want to point one specific game object?

avatar image Jix · Nov 07, 2013 at 08:56 PM 0
Share

$$anonymous$$aybe I wasn't clear enough,

I have prefab X that has a reference pointing to X (itself)

When I drag X to the scene in editor it creates instance Y, the problem is that the reference in Y is pointing to Y not X. I want it to point to X.

avatar image Khalos · Nov 07, 2013 at 09:03 PM 0
Share

What are you actually trying to accomplish here? This potentially seems like an example of the 'XY Problem'.

avatar image Jix · Nov 07, 2013 at 09:18 PM 0
Share

I don't know what you mean by 'XY Problem' but what I want to accomplish is creating a permanent reference that refers to a prefab. To understand my problem: 1-create a prefab 2- attach a script to this prefab that has a public GameObject field 3-In editor drag the prefab to the reference (the reference in the script in the prefab now is pointing to the prefab itself) 4- Now while still in editor drag the prefab to the scene to create a GameObject and check the reference in the script... it is not pointing to the prefab. It is pointing to the GameObject just created.

I don't want that, I want the reference to point to the prefab

avatar image Tanshaydar · Nov 07, 2013 at 09:31 PM 0
Share

If a prefab points to itself, it will always point to itself. You can have a game object in the inspector and put all the prefabs in the scene and select your X then hit apply, all of your prefabs in the scene will point to X.

Show more comments

2 Replies

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

Answer by Loius · Nov 07, 2013 at 08:59 PM

You have to have a manager (which could be an instantiator or a human or maybe a custom editor) assign the prefab reference. An object can't know what prefab it came from without help.

 Thing thing = (Thing)Instantiate(prefab);
 thing.prefabSource = prefab;

You might be able to [ExecuteInEditMode] something on Start that searches the project for a prefab that looks close enough to "this" and then assign that as the prefab.

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 Jix · Nov 07, 2013 at 09:11 PM 0
Share

this [ExecuteInEdit$$anonymous$$ode] is new to me, I'm gonna try something and if works I'll let you know.

avatar image Jix · Nov 07, 2013 at 11:27 PM 0
Share

Thanks for pointing me in the right direction. problem solved

avatar image
3

Answer by alexisrabadan · Oct 14, 2015 at 11:22 AM

An alternate method to having a manager script is to have a scriptable object make the connection like so:

 public class PrefabConnection : ScriptableObject {
             
             #region PUBLIC_VARIABLES
     
             [SerializeField] private GameObject prefab;
     
             #endregion // PUBLIC_VARIABLES
             
             #region PUBLIC_METHODS
     
             public GameObject GetPrefab() {
                 return prefab;
             }
             
             #endregion // PUBLIC_METHODS
         }

Then the object can use this scriptable object as the reference to its own prefab, the downside is that you have to have a scriptable object per prefab, but in my case no object will reference the newly created one so this works great. You could also create a single scriptable object that references all prefabs to make less data files in the project.

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 Jix · Oct 14, 2015 at 12:54 PM 1
Share

This question is 2 years old actually and what I did back then is to create a master prefab that points to all the prefabs.

But your answer is brilliant! It will come in handy when I face a similar problem in the future

avatar image alexisrabadan Jix · Oct 14, 2015 at 08:24 PM 2
Share

Cheers, the reason I posted though is because there is no other solutions that I could easily find

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

19 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

Related Questions

Can I create a child gameobject on a prefab with an editor script 2 Answers

Safely deleting a child of a prefab 1 Answer

Why does CreateEmptyPrefab return null? 1 Answer

Editor Script: Linking GameObjects to public script variables resets when playing. 1 Answer

Why is PrefabUtility.IsPartOfAnyPrefab(gameObject) returning false even though the object is a prefab? 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