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 Digital-Phantom · Jul 15, 2013 at 07:44 PM · errorobjectdestroy

Having problems destroying an Object

OK, so Im trying to destroy an object and cant seem to work out why its not happening. Basically Im at a terminal and want to switch a fence from activated to deactivated. Simply I want to destroy one object and spawn another in its place.

My script-

 #pragma strict
 
 var pickUp : AudioClip;
 var lockedScreen : GameObject;
 var unlockedScreen : GameObject;
 var laserGateOpen : GameObject;
 var laserGateClosed : GameObject;
 
 
 
 function Start ()
 {
 
 }
 
 
 function OnTriggerStay (other : Collider)
 {
     
     // ... and the switch button is pressed...
     if (Input.GetKeyDown("i"))
            {
             // ... deactivate the laser.                
             AudioSource.PlayClipAtPoint(pickUp, transform.position);
             Destroy(gameObject);
             Instantiate(unlockedScreen, lockedScreen.transform.position, Quaternion.identity);
             LaserFenceDeactivation();
         }                            
 }
 
 
 function LaserFenceDeactivation ()
 {
     DestroyImmediate(laserGateClosed, true);
     Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);
 }

At first I simply used Destroy(laserGateClosed) but got this error message-

 Destroying assets is not permitted to avoid data loss.
 If you really want to remove an asset use DestroyImmediate (theObject, true);
 UnityEngine.Object:Destroy(Object)
 LaserFenceControl:LaserFenceDeactivation() (at Assets/Scripts/LaserFenceControl.js:34)
 LaserFenceControl:OnTriggerStay(Collider) (at Assets/Scripts/LaserFenceControl.js:27)
 


But if I use DestroyImmediate I get this error -

 Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks. You must use Destroy instead.
 UnityEngine.Object:DestroyImmediate(Object, Boolean)
 LaserFenceControl:LaserFenceDeactivation() (at Assets/Scripts/LaserFenceControl.js:34)
 LaserFenceControl:OnTriggerStay(Collider) (at Assets/Scripts/LaserFenceControl.js:27)


Maybe Im missing something but one seems to contradict the other ?

Any ideas plz guys ???

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

2 Replies

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

Answer by iwaldrop · Jul 15, 2013 at 11:23 PM

You aren't working on instances of your Prefabs, but on the Prefab Originals themselves. @Supercouge has a good suggestion when he mentions to create the instances you are going to work with up front, and I concur. However, I wouldn't be destroying and instantiating things over and over again; just set them to inactive.

 GameObject fieldActiveGO;
 GameObject fieldInactiveGO;
 
 function Start()
 {
     fieldActiveGO = Instantiate(fieldActivePrefab, transform.position, transform.rotation);
     fieldInactiveGO = Instantiate(field=InactivePrefab, transform.position, transform.rotation);
 }
 
 function ChangeFieldState(bool turnOn)
 {
     fieldActiveGO.setActive(turnOn);
     fieldInactiveGO.setActive(!turnOn);
 }

Creating and destroying objects is very slow, so avoid it when you can. In the case of flipping things back and forth between two states, obviously, you can just turn one on and the other off.

Comment
Add comment · Show 5 · 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 Digital-Phantom · Jul 16, 2013 at 05:35 AM 0
Share

Ive not used setActive yet. Will that still work with objects/instances that use triggers and animations?

avatar image supercouge · Jul 16, 2013 at 06:47 AM 1
Share

An object which uses Trigger and Animation can, of course, be activated and desactivated. However, when an object is desactivated, it will stop to collide with other objects and it will not be animated anymore (neither rendered), until it is re-activated.

You should implement the solution of iwaldrop, it is the best way to achieve what you want to do.

avatar image Digital-Phantom · Jul 16, 2013 at 01:23 PM 0
Share

GameObject fieldActiveGO; GameObject fieldInactiveGO;

Are what exactly? Ive assumed they are meant to be(In the case of my script)-

GameObject laserGateClosed; GameObject laserGateOpen;

However this just gives me another error-

 Assets/Scripts/LaserFenceControl.js(10,11): UCE0001: ';' expected. Insert a semicolon at the end.

(If it makes any difference Im using Unity 4.1. Only saying this as when I tried to use one of the other suggestions by you guys I was getting an Obsolete message)

???

avatar image iwaldrop · Jul 16, 2013 at 03:36 PM 0
Share

Did you figure it out, or are you still getting an error?

avatar image Digital-Phantom · Jul 17, 2013 at 01:54 AM 0
Share

Still no luck with this. Seem to get a new error message each time I try something

avatar image
1

Answer by supercouge · Jul 15, 2013 at 09:33 PM

I guess that:

 var laserGateOpen : GameObject;
 var laserGateClosed : GameObject;

Are directly linked to your Prefab and not to an instantiated prefab (or clone Prefab). It is usually better to work with instantiated prefab. It allow you, for example, to destroy the "Clone" by using Destroy(), anywhere you want in your script.

So in your Start() function, you should do something like this:

 laserGateClosed = Instantiate(laserGateClosed, transform.position, Quaternion.identity);
 laserGateOpen = Instantiate(laserGateOpen, transform.position, Quaternion.identity);

(you should do it for each of your prefab in fact, and each time you Instantiate a gameObject from a prefab). This way you will hold a clone object of your prefab, and, when you will call Destroy(), you will destroy the clone and not the prefab.

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 Digital-Phantom · Jul 17, 2013 at 12:50 PM 0
Share

O$$anonymous$$.. were so close to getting this (I am that is. $$anonymous$$ost of you guys already know it...lol)

Script now reads -

 #pragma strict
  
 var pickUp : AudioClip;
 var lockedScreen : GameObject;
 var unlockedScreen : GameObject;
 var laserGateOpen : GameObject;
 var laserGateClosed : GameObject;
 var LaserGateSpawner : GameObject;
 
  
 function Start ()
 {
     laserGateClosed = Instantiate(laserGateClosed, LaserGateSpawner.transform.position, Quaternion.identity);
 }
  
  
 function OnTriggerStay (other : Collider)
 {
  
     // ... and the switch button is pressed...
     if (Input.Get$$anonymous$$eyDown("i"))
         {
             // ... deactivate the laser.          
          AudioSource.PlayClipAtPoint(pickUp, transform.position);
          Destroy(gameObject);
          Instantiate(unlockedScreen, lockedScreen.transform.position, Quaternion.identity);
          LaserFenceDeactivation();
        }                   
 }
  
  
 function LaserFenceDeactivation ()
 {
     Destroy(laserGateClosed);
     Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);
 }


Everything is working fine apart from one small thing. The LaserGateOpen is spawning(Instantiating) in the right place BUT its twisted 90 degrees on the Y axis. Ive tried changing the prefabs rotation, but that does nothing, still facing wrong way?

Shouldn't this line-

 Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);

Spawn the OpenGate at exactly the same place AND rotation of the gate its replacing ???

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

17 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

Related Questions

NullReferenceException - When destroying Object 1 Answer

Why Can't I Destroy This Object? 4 Answers

Unity crashing because of added object 1 Answer

turret not getting destroyed 1 Answer

Can't proceed next level when the object destroy not complete. 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