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 ShroomWasTaken · Jan 29, 2018 at 07:06 PM · prefabinstantiate prefabprefab-instanceinstantiationreferencing

GameObject variable points to instance instead of prefab when prefab gets instantiated

I have a component called Item which holds a reference to a Prefab. The GameObject this component is attached to is that Prefab, so it's a variable which points to its own Prefab.

The problem is that when I instantiate this prefab, the variable which references the prefab seems to actually change and point to the instance of the instantiated GameObject instead of referencing the Prefab.

I'm assuming this is because it's actually referencing itself and not the prefab object? I have no idea, but is there any way to solve this problem?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Obsessi0n · Jan 29, 2018 at 07:25 PM

I am not sure what you want but,

 theClonedExplosion = Instantiate(explosion,transform.position, transform.rotation);

If you intantiate something this way "theClonedExplosion" will point to the new intantiated object.

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 ShroomWasTaken · Jan 29, 2018 at 07:40 PM 0
Share

I think I explained my issue kinda bad, but in this case it would be the explosion variable which changes from a reference to a prefab to a reference to an instance due to the fact that it was pointing at the Prefab which is being instantiated using that line. I can't seem to explain in a good way :/

avatar image Obsessi0n ShroomWasTaken · Jan 29, 2018 at 08:30 PM 0
Share

What @Commoble said is right there should be another simpler way to do what you want.

avatar image
0

Answer by Commoble · Jan 29, 2018 at 07:28 PM

I believe this is intended behaviour. Look at it this way: Your prefab is a GameObject that holds a component, and that component has a field that points to the GameObject that the component is attached to. When you instantiate the prefab, you create a copy of that object, and so the copy also contains a component that points to its own GameObject. Make sense?


If you want a long-term reference to your prefab so you can easily instantiate it at will, you should use a different object to keep a reference to it (also make sure you're not assigning the newly instantiated copy to your field).

Comment
Add comment · Show 3 · 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 ShroomWasTaken · Jan 29, 2018 at 07:45 PM 0
Share

Hm yeah that's what I thought was the case.
I don't think I would be able to do that because of how the code works. Basically what I'm creating is an item system, and this solution would require every item in the game to have a second object to reference the prefab, which doesn't seem like a very good approach.

But I guess I might have to redo the system if this problem can't be solved without increasing the amount of objects :/

avatar image Commoble ShroomWasTaken · Jan 29, 2018 at 08:36 PM 0
Share

I was thinking more along the lines of having a single manager object that instantiates all of your prefabs (or at least all prefabs of a related set)

I use a ScriptableObject to hold a set of prefabs and then give a manager gameobject a reference to that scriptable object

avatar image ShroomWasTaken Commoble · Jan 29, 2018 at 08:59 PM 0
Share

Not 100% sure if I understand, but the thing with my current system is that the object which I'm instantiating needs a reference to the prefab it was instantiated from.

I'm trying to make a item system where Item is a normal component and holds a object of type ItemData, which contains a GameObject which is set to the prefab. When the Item gets picked up the ItemData gets copied to the inventory, and the Item GameObject gets destroyed (and since the prefab of ItemData references the instance ins$$anonymous$$d of the prefab it becomes null). That way if the player drops a specific ItemData from the inventory it can be re-instantiated into the scene again. But now it can't since the prefab ins$$anonymous$$d references the instance.

Not sure if that makes any sense or helps at all. $$anonymous$$aybe I should add my current code to the post?

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

93 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

Related Questions

Instantiated prefab references point to first prefab instantiated 3 Answers

Accessing instance of prefab from its own script 1 Answer

Why is it important to create an empty gameobject for my prefabs? 0 Answers

How can I create a prefab variant in C# script? 1 Answer

instantiated GameObject Prefab jumps in air after instantiate 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