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
0
Question by ozavimal · Jan 27, 2013 at 11:30 AM · gameobjectprefabdestroy

Create a prefab at same position where its destroyed

I am new to Unity3D gaming. I've an idea to create the same object/prefab at the same place where its being destroyed. I wrote a script but doesn't work for me. var thePrefab : GameObject; function Start (){ } function Update (){ }

 function OnTriggerEnter () {
     Destroy(gameObject);   
     yield WaitForSeconds (2);
     reGenerateTheCoin();
 }
 function reGenerateTheCoin ()
 {
     Instantiate (thePrefab,transform.position,transform.rotation);
       Debug.Log("Regenerated the Coin");
 }

Thank you in Advance for your help.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FL · Jan 27, 2013 at 12:10 PM

If you destroy the object, you can't reference its tranform, so save it in variables.

 var thePrefab : GameObject;
 private var oldPosition : Vector3;
 private var oldRotation : Quaternion;
 
 function Start (){ 
 }
 
 function Update (){ 
 }
 
 function OnTriggerEnter () {
     oldPosition = transform.position;
     oldRotation = transform.rotation;
     Destroy(gameObject);   
     yield WaitForSeconds (2);
     reGenerateTheCoin();
 }
 function reGenerateTheCoin ()
 {
     Instantiate (thePrefab,oldPosition,oldRotation);
     Debug.Log("Regenerated the Coin");
 }
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 ozavimal · Jan 27, 2013 at 05:10 PM 0
Share

Thank you @FL for the quick reply.. But still I am facing the Same issue for not instantiating the Prefab. Could you analyse it again and suggest me.

avatar image FL · Jan 29, 2013 at 11:51 PM 0
Share

The log is working? If no, try to put this script at an object with one trigger collider.

avatar image
0

Answer by Bunny83 · Jan 30, 2013 at 12:10 AM

This line:

 Destroy(gameObject);

Will destroy your gameobject as well as your script. The coroutine will be destroyed as well since it's running on your script which has been destroyed already. Also usually you can't reference a prefab from a script that is part of the prefab (if i understood you right).

The problem is that references which reference other things inside the prefab are changed to the actual instantiated objects when the prefab is instantiated. So if a script on a prefab holds a reference to it's own prefab, the reference would just reference the cloned object and not the prefab.

A solution which would fix both issues would be to use a second prefab with another simple script:

 // HelperScript.js
 var prefab : GameObject;
 var waitTime = 2.0f;
 
 function Start()
 {
     yield WaitForSeconds(waitTime);
     Instantiate(prefab,transform.position, transform.rotation);
     Destroy(gameObject);
 }

Create an empty GameObject, attach this script, assign the prefab you want to instantiate and turn that object also into a prefab called "Helper" for example.

Now the script on your original prefab wouldn't reference it's own prefab, but the Helper prefab. With this setup you would do this:

 function OnTriggerEnter ()
 {
     Instantiate (theHelperPrefab, transform.position, transform.rotation);
     Destroy(gameObject);
 }

Now when you enter the trigger the helper prefab will be created and the object will be destroyed. After the set time (2 sec) the helper prefab will instantiate a new instance and destroy itself.

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 manash2014 · Oct 21, 2014 at 05:51 PM 0
Share

is it possible for a c# version?

avatar image manash2014 · Oct 21, 2014 at 05:55 PM 0
Share

also I am trying to get this done through void mousedown

public GameObject lightningball2;

 void On$$anonymous$$ouseDown()
 {
     Destroy (gameObject);
     Instantiate(lightningball2, transform.position, transform.rotation);
 }

when I destroy my object and instantiate a prefab, the prefab does not pop up at the same position where I destroy my object, it shows up somewhere else, and I don't know what position is unity calculating to position my prefab instance.

avatar image
0

Answer by ian007 · May 03, 2014 at 01:37 AM

I think the best approach for this would actually be to set the game object to be inactive and then reactivate when the player collides with a trigger. I'm new to scripting in unity but im pretty sure the game object will not move after it is set inactive, that will make it to where you can "save" that game object's position

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

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

12 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Trying to get bullet to collide and be destoryed 1 Answer

How many time does a prefab takes to load?? 1 Answer

Collision Detection for a Prefab 1 Answer

Deleting a GameObject 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