• 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 { SceneManager.UnloadScene(0); SceneManager.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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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

Related Questions

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

Instantied 2D Prefab Is Invisible 1 Answer

How do I instantiate certain objects to appear in a specific spot? 3 Answers

The prefab you want to instantiate is null. 2 Answers

Issues with instantiated prefabs 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges