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 Sawula · Jan 02, 2015 at 06:24 PM · collidertriggeractivegame object

On trigger active object, else not c#

Hey guys, sorry to bother. I have two gameobject in my game, one with a trigger, one with a render. I'd like the trigger to activate the render when something is touching it, but to deactivate it when it's not. I thought I had it nailed with this using UnityEngine; using System.Collections;

 public class warning1 : MonoBehaviour {
 
     public GameObject myrender;
 
     void OnTriggerStay () {
         Debug.Log ("Warning!");
         myrender.SetActive (true);
     }
     
     void OnTriggerExit () {
         myrender.SetActive (false);
     }
 }

But it doesn't work. When something enter the collider it does turn on the render object, but it doesn't turn it off when the collider is empty again...

EDIT: I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. That way, when my enemies reach the trigger, the render should go on and alert my players, and once he destroy them, the render should go off. The log tell me my enemies entering the trigger is detected, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want. It seems as if the trigger doesn't detect my enenmies destruction as a way of leaving the trigger.

Comment
Add comment · Show 4
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 Qasem2014 · Jan 02, 2015 at 07:37 PM 0
Share

maybe you need a flag (boolean variable) to check the state and check that flag in Update function to active or deactive something !

avatar image kjuuz · Jan 02, 2015 at 07:49 PM 0
Share

How you can enter in something if it's not even active? You would like to disable rendering by "renderer.enabled = false", but if object have physics it will interact while invisible anyway.

avatar image Mmmpies · Jan 02, 2015 at 07:53 PM 0
Share

It looks like your code should work. Add a Debug.Log to the Exit to show when that's being called. I can only think that Stay is somehow still triggering on the same frame that Exit fires. You could also try OnTriggerEnter ins$$anonymous$$d of OnTriggerStay as that only fires when it first enters.

avatar image Sawula · Jan 02, 2015 at 09:55 PM 0
Share

The object with trigger is always active. It's the object with render that I want to turn on and off. I know I could just turn the render on and off, but it seems easier to turn the whole thing off. Less referencing code to do.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Anxo · Jan 02, 2015 at 07:52 PM

You might not be hitting what you think you are. Try this.

 void OnTriggerEnter(Collider other) {
        Debug.Log(other.transform.gameObject.name);
     }
 
 void OnTriggerExit(Collision collisionInfo) {
    Debug.Log(collisionInfo.transform.name);
 }

WUCC

See if the results are as expected.

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 Sawula · Jan 03, 2015 at 11:52 AM 0
Share

I tried, and yes, it is the right item that collide. I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. The log tell me that's what happen upon entry, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want... I really don't get what I do wrong...

avatar image Anxo · Jan 03, 2015 at 01:21 PM 0
Share

Ah so that is useful information, so it works if you move out of the trigger but exit is not called if the other collider is destroyed.

In that case you can set up a method that handles the OnTriggerExit outside of the method itself so you can handle it when the enemies are destroyed.

 void OnTriggerEnter(){
    myRenderer.SetActive(true);
 }
 
 void OnTriggerExit(Collision collisionInfo){
     HandleTriggerExit(collisionInfo.transform.gameObject);
 }
 
 void HandleTriggerExit(GameObject enemy){
     myRenderer.SetActive(false);
 }
 
 void Some_$$anonymous$$ethod_That_Destroys_Enemies(){
      HandleTriggerExit(someEnemy);   // Then you can call this from anywhere to turn off the renderer before you destroy the enemy.
      Destroy (someEnemy);
 }

Warning Uncompiled Code

avatar image Sawula · Jan 03, 2015 at 02:46 PM 0
Share

I'm sorry but I'm not that well versed in Unity yet, so I'm not quite sure how to use the code you gave me... I have a script to kill my enemies, with a void $$anonymous$$ill method, I tried to place that ins$$anonymous$$d of the 'Some-method' you wrote, but it doesn't work. I'm probably doing something wrong, I know...

avatar image
0

Answer by ChrPunx · Jan 02, 2015 at 08:41 PM

 void OnTriggerEnter(Collider Trigger){
     if(!myRender.activeInHierarchy && trigger.collider.gameObject.tag == "targetTag"){ // if you  want to activated only for a specific object with a specific tag
         myRender.SetActive(true);
         Debug.Log("Render activated");
     }
 }
 void OnTriggerExit(Collider Trigger){
     if(myRender.activeInHierarchy && trigger.collider.gameObject.tag == "targetTag"){ // if you  want to deactivated only for a specific object with a specific tag
         myRender.SetActive(false);
         Debug.Log("Render deactivated");
     }
 }

Comment
Add comment · Show 7 · 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 Sawula · Jan 03, 2015 at 11:55 AM 0
Share

Your code allowed me to realize something. I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. The log tell me that's what happen upon entry, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want. It seems as if the trigger doesn't detect my enenmies destruction as a way of leaving the trigger.

avatar image ChrPunx · Jan 03, 2015 at 01:12 PM 0
Share

does you enemies have a Health script on them? if so, add something like...

 OnTriggerStay(...)
 if(col.collider.gameObject.getComponent<Health>().dead == true){
  // what to do
 }
 (...)
avatar image Sawula · Jan 03, 2015 at 02:47 PM 0
Share

Yes they do,but I'm not that well versed in Unity yet, so I'm not quite sure how to use the code you gave me... Unity say col. is something unknown to it...

avatar image ChrPunx · Jan 03, 2015 at 02:53 PM 0
Share

place a bool on your Health script, and this on your Update method

if(health <= 0){ dead = true; }

/// on the trigger script

 void OnTriggerStay(Collider triger){
 if(col.collider.gameObject.getComponent< your health script>().dead == true){
   myRenderer.SetActive(false);
 }
 }


avatar image Sawula · Jan 03, 2015 at 03:23 PM 0
Share

$$anonymous$$y health script is called Life, in it I put a bool, like you said, called dead. and I added the line you gave me in Update.

Then I copied and pasted the trigger code into my trigger script, but since it kept saying 'col.' doesn't exist, I tried fiddling it, see if I could make it work. Tried this

     void OnTriggerStay(Collider other){
         Life l = other.gameObject.GetComponent<Life>();
         if (l.currentLifePoints <= 0){
             myrender.SetActive(false);

it tells me the instance isn't declared. Same for this

 void OnTriggerStay(Collider other){
      Life l = other.gameObject.GetComponent<Life>();
         if(l.dead == true){
         myrender.SetActive(false);

And this one same thing

 void OnTriggerStay(Collider other){
     if(other.gameObject.GetComponent<Life>().dead == true){
         myrender.SetActive(false);
Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Should Physics.Raycast() hit a Trigger on a disabled GameObject? 1 Answer

Triggering objects to be Active 2 Answers

Why setting an active object to false returns error of 'Object reference not set to an instance of an object' 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Triggering platform animation on colliding with Button 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