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 PeeG · Sep 14, 2012 at 10:39 AM · transformdestroygame object

Destroy and Instantiate

I should precede my question by saying I am Unity newbie so am trying to learn many things with regards to it, but am unsure with an issue I am getting here and haven't been able to find a solution. It is using a game / code that had already been written by someone else in a previous version of Unity (not in contact with that person anymore and unsure on exact version). Previously this part of the game was working, but upon opening the project in the version 3.4.2 this aspect of the game does not work correctly now.

To get to the point of the problem, I have a jetfire that when it collides with a wall it creates another particle stream of fire and smoke rising. This still works fine currently, but when you now move the location of the wall instead of the fire and smoke that arose due to collision being destroyed it remains in place.

Originally, the code used the following

 Destroy(instantiatedFire);
 Destroy( instantiatedSmoke );

This in the new version of Unity gave the error message that it could not destroy a transform. Through searching through other similar questions asked I changed this code to the following:

 Destroy( (instantiatedFire as Transform).gameObject );
 Destroy ( (instantiatedSmoke as Transform).gameObject );

This instead of just destroying the instantiated fire and smoke when you moved the wall, as it should have done, it destroyed it to the extent that it wasn't even created even though there is code following it to instantiate the fire. Part of the following code here:

 var instantiatedFireOrigin = hit.point + hit.normal*0.75;    
 instantiatedFire = Instantiate(radialFire, instantiatedFireOrigin, Quaternion.identity);
 var rotationAxis : Vector3 = instantiatedFire.transform.TransformDirection(1, 0, 0);
 instantiatedFire.transform.rotation = Quaternion.FromToRotation(rotationAxis, hit.normal);

Would be grateful for any feedback on this to hopefully try and sort this issue.

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 Sisso · Sep 14, 2012 at 11:42 AM 0
Share

Please, write less and directly if you want a answer. Long histories scare most readers.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Sep 14, 2012 at 11:42 AM

You can only remove GameObjects, Transform is a component of a GameObject. There many docs around about Componet Base development, like http://docs.unity3d.com/Documentation/Manual/UsingComponents.html.

I think that the problem is not inside these codes, you are almost there. Could be better create TestScene were you could test each code separated. You could debug using proper name, step foward and Debug.Log messages.

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 PeeG · Sep 14, 2012 at 12:07 PM 0
Share

Thanks for feedback Sisso. Will take note for next time. Will check that documentation out. I had thought that it could be an issue outside of the code since there is a lifetime assigned to these particles which seems to be getting ignored as well.

I am getting a NullReferenceException error arising from the same line of destroy code, as referenced above, which is part of an if statement further on in the code, but don't know if that issue is related.

avatar image
0

Answer by Sisso · Sep 14, 2012 at 02:25 PM

Second try :P

I understand that you instantiate many "radialFire" GameObject, but you store only the last reference. So, when you call Destroy(instantiatedFire), you are destroing only the last instance.

If was it, I see two fast solution.

1) Change your into an array, sample code:

 // declare var
 var instantiatedFires = new Array();
 
 // create 10 objects
 for (var i = 0; i < 10  ; i ++) {
   var fire = Instantiate(radialFire);
   instantiatedFires.add(fire);
 }
 
 // to delete all
 for (var fire : GameObject in instantiatedFires) {
   Destroy(fire);
 }
 
 Reference about arrays:
 http://docs.unity3d.com/Documentation/ScriptReference/Array.html

2) Put a script inside your radialFire that destroy self when its needed.

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 PeeG · Sep 14, 2012 at 02:54 PM 0
Share

Using that bit of code as a starting point I get "Unexpected token: &" and "expecting 'p'. found '6' " with reference to the declare var line. I'm not familiar with the use of "=" and "+" to identify a workaround here.

avatar image Sisso · Sep 14, 2012 at 03:16 PM 0
Share

It's simple sample code, don't expect copy paste and it works :P

avatar image PeeG · Sep 14, 2012 at 03:26 PM 0
Share

Schoolboy error on my part. Can't really use the not knowing the format of the site here as it should have been the first thing I checked. You are right that radialFire clones keep on getting instantiated when you move the wall so will work on this to be able to destroy all the previously called clones.

Thanks for your help.

avatar image PeeG · Sep 17, 2012 at 02:52 PM 0
Share

I have had a look at this more and watched it more in the inspector. Only one radialFire transform (not game object) is created when you move the wall. If I put in any form of a Destroy command in treating the radialFire or instantiatedFire transforms as a gameObject it destroys it to the extent that it isn't even created despite the instantiate command being called after the Destroy command in the if statement. I can't understand why the radialFire component is not 'created' again here. I get a NullReferenceException on this line further on as well:

instantiatedFire.GetComponentInChildren(SandboxFireEvent).RunFireEvent();

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

10 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

Related Questions

Bug: cannot assign public Transform variable. Unity 2D 5.2 1 Answer

How to turn object into a particle system and back? 0 Answers

Is "null" the best way to unparent a child? 1 Answer

Destroy vs Don't Destroy 2 Answers

Script problems... 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