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 Al-Anselmo · Dec 27, 2010 at 10:59 PM · cameratriggerbugimage-effects

OnTriggerEnter problem if disactivating gameobjects

Is it really possible to disactive gameObjects when something triggers? Specifically I'm talking about a Camera with an Image Effect enabled.

With the code below, I get the following error:

Destroying object immediately is not permitted during physics trigger and contact callbacks. You must use Destroy instead. UnityEngine.Object:DestroyImmediate(Object) SSAOEffect:DestroyMaterial(Material) (at Assets/Standard Assets/Image Effects (Pro Only)/SSAOEffect.cs:41) SSAOEffect:OnDisable() (at Assets/Standard Assets/Image Effects (Pro Only)/SSAOEffect.cs:49) UnityEngine.GameObject:set_active(Boolean) CameraSplit:OnTriggerEnter(Collider) (at Assets/Scripts/CameraSplit.js:50)

function OnTriggerEnter(other : Collider)
{
    if (other.gameObject.CompareTag ("Player"))
    {
        Camera.main.gameObject.active = false;
    }
}

If I disable the effect before playing the scene, the error doesn't show up immediately, but after some time it does.

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 Al-Anselmo · Dec 27, 2010 at 11:58 PM 0
Share

I learned that this error shows up after entering the trigger for the second time. If I enter the trigger, keep walking and go back, colliding with it again, I egt the error.

2 Replies

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

Answer by Al-Anselmo · Dec 28, 2010 at 01:15 AM

I'll answer my own question because I did something that fixed my problem. If someone wants to add a more detailed answer, feel free.

I continued working on it. I wanted to disable the Camera and AudioListener components as soon as the player entered the trigger zone.

I added

var mainCamera : Camera;

and with the OnTriggerEnter, I did:

function OnTriggerEnter(other : Collider) { if (other.gameObject.CompareTag ("Player")) { mainCamera.enabled = false;

     mainCameraAudioListener = mainCamera.GetComponent(AudioListener);
     if (mainCameraAudioListener.enabled == true)
     {
         mainCameraAudioListener.enabled = false;
     }
 }

}

With this I could start the game with those mainCamera components enabled, and they would be disabled (enabled = false) when I got into the trigger zone. After leaving and entering the same zone again, I would get no errors at all. Problem fixed.

Just a curiosity: Why does mainCamera.enabled = false; works and

if (Camera.main.enabled == true)
{ 
    Camera.main.enabled = false;
}

doesn't? This seems to be almost the same.

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
avatar image
1

Answer by heks · Dec 27, 2010 at 11:35 PM

Disabling a GameObject during a collision/trigger is perfectly fine, however as the error message points out destroying objects immediately during a trigger is not permitted.

When disabling a GameObject, any scripts attached to it will get a call to a OnDisable() method. In this case the SSAOEffect script's OnDisable() is being called, and this subsequently tries to call DestroyImmediate() - which spawns the error message.

The easiest solution would be to delay the deactivation until the next Update(), in which case you're guaranteed not to be inside collision/trigger updates.

Comment
Add comment · Show 3 · 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 Al-Anselmo · Dec 27, 2010 at 11:45 PM 0
Share

How do I do that?

avatar image vexe · Sep 25, 2013 at 07:12 AM 0
Share

Just came up with the same error, here's how you do what @heks suggested.

avatar image usagi · May 26, 2014 at 01:36 PM 0
Share

Hi vexe, I red the solution you proposed but I wasn't able to resolve the issue (with neither WaitForEndOfFrame, nor WaitforSeconds).

Could you explain more in deep the solution you adopted?

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

2 People are following this question.

avatar image avatar image

Related Questions

Creating a racing relay. 0 Answers

rotate with alt doesnt work 15 Answers

How you change another script value smoothly on trigger 0 Answers

Wrong camera position/orientation on windows 10 store build: How to fix it? 0 Answers

Ngui Camera and Image effects 0 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