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 /
avatar image
0
Question by FarbrorMartin · May 15, 2014 at 10:17 PM · c#instantiateprefabinstance

Possible Bug? GameObject.Instantiate copies instance instead of Prefab

Hi!

I am having trouble with spawning prefab instances from a script attached to that same prefab. Any changes I make to the instance gets are also present in any new prefab instances I create.

I am making an endless side scroller with some random items. I have a prefab "Section" that I use for making new sections of the background as the player moves forward. I have a script on the Section prefab that spawns a new Section when the player enters a trigger zone in the section. I also spawn some random flavour details in the section in Start().

The problem is that the details i spawn in the current instance also get added to the new Section instances that gets spawned in the trigger event. The first section spawned looks ok, but for each following section, all the previous detail items remain and new ones get added.

First I thought that the detail items I spawn in Start somehow get added to the Prefab and not just the instance. So when I later spawn a new section from the prefab, the new instance contains all the details from the current instance.

But if I look at the prefab in the editor while the game is running, it is clean, and doesn't contain all the spawned items. So it seems the GameObject.Instantiate actually instantiates the current instance instead of the prefab I have assigned in the editor.

I can also see that if I click the SectionPrefab property in the editor, the instance of that prefab in the scene is highlighted, and not the prefab in the project browser.

Am I doing something wrong? Is this expected behaviour?

Is there a way to explicitly reference the prefab in the instantiate call?

My script looks something like this:

 // Script attached to the Section prefab
 class Spawner : MonoBehaviour
 
 public Transform SectionPrefab; // Assigned the Section prefab in editor.
 public Transform DetailPrefab; // Assigned in editor
 
 Start()
   var detail = GameObject.Instantiate(DetailPrefab, MakeRandomPosition(), Quaternion.identity) as Transform; //Spawn detail item and add as child
   detail.parent = transform; // this should add the detail item to the current instance

 OnTriggerEnter()
   // this should instantiate from the prefab
   // but actually instantiates the current instance instead
   var pos = new Vector3(transform.position.x + 100f, transform.position.y, transform.position.z);
   var newSection = GameObject.Instantiate(SectionPrefab, pos, Quaternion.identity); // Spawn new section from Prefab


Thanks, Martin

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 Danil_3103 · May 02, 2016 at 09:37 AM 0
Share

I have same problem. I create prefabs. When game is over i'm using my Replay function that contains { Scene$$anonymous$$anager.UnloadScene(0); Scene$$anonymous$$anager.LoadScene(0); } So i get new created prefabs and prefabs that created before.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · May 16, 2014 at 12:24 AM

Take another look at the Unity Script Reference.

Use the poly-parameter version of the function, like this:

 public GameObject prefabObject;//assigned in editor
 public GameObject cloneObject;
 
 void CreateClone() {
      cloneObject = Instantiate(prefabObject, transform.position, Quaternion.identity);
 }



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 FarbrorMartin · May 16, 2014 at 05:45 PM 0
Share

Thanks, but I already use that version of the function. I didn't post my actual code because I wanted to keep it simple. I have updated my question to better match my real code.

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

22 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

Related Questions

How to get all objects instantiated from a single prefab? 1 Answer

How to randomly instantiate other prefabs parallel? 1 Answer

Updating a variable on a script in an instanced object 1 Answer

How to create new tail in a simple 3D Snake 0 Answers

Why is my Prefab Instantiating when the Scene is Loaded? 2 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