Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by aWolfKing · Apr 11, 2016 at 03:44 PM · scripting problemcollidersgameobjectsother

OnCollisionEnter for other gameobjects (C#)

What i want to code (C#) is a trigger that activates, for example a timer, with an OnTriggerEnter. However i want to stop the same timer with an OnTriggerEnter function for an other gameobject. What i think (and hope) it whould look like:

 public gameobject _Start; //(gameobject or Rigidbody etc?)
 public gameobject _End;
 
 private bool thebool;
 
 void _Start.OnTriggerEnter(Collider otherobject)
 {
 thebool = true;
 }
 
 void _End.OnTriggerEnter(Collider otherobject2)
 {
 thebool = false;
 }

This however, does not work so my question is: 'How whould I make this work?' I would apreciate any help and hope someone could help me.

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 aida12_99 · Apr 11, 2016 at 07:02 PM 0
Share

@aWolf$$anonymous$$ing you can add a boolean to your code. The boolean could be used as a flag (or a semaphor) to say whether the timer is activated by another object. So, check in OnTriggerEnter of object "_End": if OnTriggerEnter of object "_Start" has activated the flag then stop the timer in _Start. You can do it vise versa.

avatar image aWolfKing · Apr 11, 2016 at 07:20 PM 0
Share

@aida12_99 Thanks, I think this will work as an alternative. I wanted to use 1 script for this, but I guess it is not possible so I will use this;

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ali-hatem · Apr 11, 2016 at 04:20 PM

  void OnTriggerEnter(Collider other)
  { 
      if(other.gameObject.name == "object1"){
      // do something 
      }
 
      if(other.gameObject.name == "object2"){
      // do something 
     }
 }

you can replace gameObject.name with gameObject.tag if you deal with many objects that you want something to happen when one of them entered the area but you have tag them in inspector first .

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 aWolfKing · Apr 11, 2016 at 05:57 PM

@Ali hatem Sorry, i think you misunderstood me; What i meant is: OnTriggerEnter only applies to the gameobject the script is placed in. I want to know how i would check if somthing collides with gameobject a if the script is in gameobject b. I appreciate your help, but this was not what i meant (I am not very skilled in explaining, sorry).

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 Ali-hatem · Apr 11, 2016 at 07:47 PM 0
Share

in gameobject a you can cheek if the collided object is gameobject b & vice versa gameobject a script :

   void OnTriggerEnter(Collider other)
   { 
       if(other.gameObject.name == "objectB"){
       // do something 
       }
  }

but if still not what you want please explain what exactly you want to do like if Enemy collide with Player & something should happen so the picture will be clear .

avatar image aWolfKing · Apr 12, 2016 at 06:07 AM 0
Share

@Ali hatem Sorry, still wat i meant. If you put an script with OnTriggerEnter in it, that void will 'start' when something collides with the gameobject the script is in. However, what I want is an OnTriggerEnter void that 'starts' when something collides with an other gameobject than the gameobject is in. I hope I made clear what I want and thank you for you help.

avatar image Ali-hatem aWolfKing · Apr 12, 2016 at 08:31 AM 0
Share

1- it's the same you don't need to put a script to Game Object you want to deal with if it collide with Game Object have a script with OnTriggerEnter for example :

 void OnTriggerEnter(Collider other)
    { 
        Destroy(gameObject);
        // other means the Game Object has collided with this Game Object.
        Destroy(other.gameObject); 
        }
   }

2- but if you mean OnTriggerEnter should be called in object because of other 2 objects collided with each other it's impossible if you want to detect collisions you have to put OnTriggerEnter in object & then you can apply any thing to the object that have the OnTriggerEnter founction or to the other object that collided with it but you can put a bool to be true when they collide if you want to detect there collisions in a third object.

3- lastly back to your main question if you want object 1 to activate timer when collide with something & object 2 deactivate timer when something collide with it then you should have OnTriggerEnter in both object 1 & object 2 :

 // object 1 script
 void OnTriggerEnter(Collider other){
 // activate timer
 }
 
 // object 2 script
 void OnTriggerEnter(Collider other){
 // deactivate timer
 }
avatar image aWolfKing · Apr 12, 2016 at 08:55 AM 0
Share

@Ali hatem The 3rd one is what i meant, I was wondering if that was possible in just 1 script. Your and the answer aida gave me answered my question, it is not possible. Thanks for your help and I will use multiple scripts to achieve what I wanted to code.

avatar image Ali-hatem aWolfKing · Apr 12, 2016 at 10:50 AM 0
Share

it's possible if you place OnTriggerEnter in the object that collide with the object 1 & object 2 by testing there name or tag & activate or deactivate depend on that so look at my answer & consider it's on the object that collide with the object 1 & object 2 .

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

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

Related Questions

How to attach two colliders (i.e two or more game objects )? 1 Answer

Issue With Input.GetKey and IENumerator in Grabbing Script 1 Answer

How to find all gameobjects that use the same material and change it? 1 Answer

How to generate objects from a script in the editor, i.e. before runtime? 1 Answer

Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 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