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 Clarinet · Mar 18, 2012 at 02:43 AM · gameobjectinstantiatedestroy

How to insert an object after Destroy(gameObject)

Hello, I was wondering for quite some time how to, after the destroy function is called, insert another game object. I've tried the Instantiate function but it doesn't seem to work. Here is the script that I'm using:

 function OnTriggerEnter(hit : Collider)    
 {
 Destroy(gameObject);    
 }

So I was just wondering if it is at all possible to create another game object right after this function... and I would also like to know how to make the other object appear after the destroy is called, but in another script.

Comment
Add comment · Show 2
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 fafase · Mar 18, 2012 at 03:19 PM 0
Share

Nope but you can create it right before.

avatar image Clarinet · Mar 18, 2012 at 03:22 PM 0
Share

Okay thanks! Do you know of a function that could do that? The only one I know is Instantiate but I've tried again and again but I don't know how to work it. The documentation is confusing...

2 Replies

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

Answer by fafase · Mar 18, 2012 at 04:06 PM

var prefabOfYourObject : Transform; // here you drag the prefab in the inspector

var newObject = Instantiate(prefabOfYourObject, transform.position,Quaternion.identity);

var newObject means you want to declare a new variable and this variable will be given the information coming in Instantiate(1,2,3);

1= the prefab of the object you want to create

2= where you want to appear, in this case transform.position it is taking the position of the object the script is attached to. You can give any value as long as it is a Vector3(x,y,z).

3= The rotation of the object when created, Quaternion.identity means you want it to appear as the prefab is (more or less), Quaternion is pretty complicated and deals with complex number so.... Here you could give any kind of other rotation there, if you want your guy upside down or else.

Comment
Add comment · Show 4 · 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 fafase · Mar 18, 2012 at 04:08 PM 0
Share

The good idea would obviously to be that your prefab has a script attached to it so that your new object is not just there counting clouds.

avatar image Clarinet · Mar 18, 2012 at 05:01 PM 0
Share

Okay, that seemed to work after I fixed a bunch of errors, but there's still a small problem. After using the script you provided (which I altered a bit), which was

var object : Transform;

function OnTriggerEnter(hit : Collider){

Instantiate (object, transform.position, transform.rotation); }

It makes the instantiated object exactly where the object you touch is. I've tried many things but I cannot get it to appear anywhere else but where that object is...

avatar image fafase · Mar 18, 2012 at 05:23 PM 0
Share

As I said transform.position is the position of the object the script is attached to (the trigger collider). If the collision happens at (10,1,10) then your new object will appear at (10,1,10) if you want it to appear somewhere else just use var newObject = Instantiate(prefab, Vector3(x,y,z),Quaternion.identity);

"(before you even touch it)" that is not possible to happen before you enter the collider so you should check the size of both colliders.

avatar image Clarinet · Mar 18, 2012 at 06:37 PM 0
Share

Oh I'm sorry. I didn't understand at first. It worked! Thank you so much!

avatar image
1

Answer by Kleptomaniac · Mar 18, 2012 at 03:24 AM

You can't use any other calls after the destruction of the gameObject, because the script will also be destroyed along with the gameObject. If you want to do something after you have destroyed a gameObject, it's as simple as not attaching the script to the object you want to destroy, but to an empty gameObject. :)

Hope that was clear enough, Klep

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 Clarinet · Mar 18, 2012 at 03:11 PM 0
Share

Alright! Thanks! That does make sense.

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

5 People are following this question.

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

Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 1 Answer

Having problem with Destroying GameObject 0 Answers

Destroying 2D Array of Instantated Objects 2 Answers

Accessing children of an instance doesn't work every time 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