Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by kushalagrawal890 · Sep 08, 2020 at 07:44 PM · prefablistinstanceprefab-instance

Can we anyhow store a prefab in a list when we collide into the instance of that prefab casted as gameObject?

I want a list to store the original prefab of a game object when my player runs into that game object instance of a prefab.

Comment
Add comment
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

1 Reply

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

Answer by streeetwalker · Sep 09, 2020 at 07:04 AM

@kushalagrawal890, Check out PrefabUtility.GetPrefabParent -- look at the documentation on this - it is supposed to return exactly what you want...

[Edit & correction]

This is an editor only function. There is no way to programmatically determine what prefab an object instance was Instantiated from. This does work in an Editor script, but not at runtime.

You can, at the time you create the instance store a reference to the prefab in a script on the object. (probably the simplest method to track and object and it's prefab.)

So, create a script on your instanced object with a public GameObject myPrefab field.

Then at the time you Instantiate the prefab:

       // assume you have a prefab reference in a public field thusly:
       public GameObject prefabObject;
       // then assume you have a script "ObjectScript.cs" as a 
       //component on the prefab that contains this:<br><br>
       public GameObject prefabObject;
       // then where you instantiate the object
       GameObject gO = (GameObject) Instantiate( prefabObject );
       ObjectScript oS = gO.GetComponent<ObjectScript>();
       // now store a reference to the prefab on the newly
       //instantiated gameObject:
       oS.prefabObject = prefabObject;


There are definitely other ways to do it. For example, if you want to store prefabs in a list, and in a corresponding list the gameObject instances, and associate the two list by their indices, for one.

Comment
Add comment · Show 8 · 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 kushalagrawal890 · Sep 09, 2020 at 11:11 AM 0
Share

Thank you for helping me. But I seem to encounter a problem. I want a list to contain prefabs so I made the list this way List name = new List(); The code also seems to be right but when I add it to the list it increments the list by one(I used prefabutility.GetPrefabParent to add) but it also says None(Object).

avatar image streeetwalker kushalagrawal890 · Sep 09, 2020 at 11:37 AM 0
Share

We'd have to see your code at this point - it's not clear what you mean.

avatar image kushalagrawal890 · Sep 09, 2020 at 07:52 PM 0
Share
 public List<UnityEngine.Object> team$$anonymous$$ates = new List<UnityEngine.Object>();
 public void AddTeammate(UnityEngine.Object teammate)
     {
         team$$anonymous$$ates.Add(teammate);
      }

AddTeammate(PrefabUtility.GetPrefabParent(this.gameObject));

avatar image streeetwalker kushalagrawal890 · Sep 10, 2020 at 03:15 PM 0
Share

OK, so you're getting an error? What exactly is the error and exactly which line of code is generating it?

avatar image kushalagrawal890 streeetwalker · Sep 10, 2020 at 07:53 PM 0
Share

I am not getting any error it's just that the list increments by 1 but doesn't store any prefab

Show more comments

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

254 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 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 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

how to change a texture on instantiated prefab? 0 Answers

Add prefab to list based on object touched? 0 Answers

How to affect only one object out of many objects with the same name using script? 0 Answers

After being instantiated once and then destroyed, prefab is null 0 Answers

How to handle having arrays in a prefab when applying a change? 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