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 /
  • Help Room /
avatar image
0
Question by robbyisdead · Dec 01, 2015 at 02:39 PM · c#unity 5collidersontriggerenter

Setting OnTriggerEnter to a second object collider

I like to keep these simplified instead of trying to explain everything that I'm doing, so here's the watered down version:

  • I have an object, called Object A

  • I have another object, called Object B

  • In its script, I tell Object A to do something if a bool is true

  • This bool only equals true if the player moves into the triggered collider of Object B

However, I don't really know how to do this. I was going to use OnTriggerEnter but with (ObjectB Collider other) instead of just (Collider other), though that doesn't seem to work. I'm working in C#. Anyone have any ideas?

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
0

Answer by itsharshdeep · Dec 01, 2015 at 03:34 PM

Hello

Sory I didn't understand the last part of the question. Can you please check the following link that whether is this you want?

http://answers.unity3d.com/questions/1102512/im-very-new-to-scripting-and-here-is-my-question-w.html#answer-1102756

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
0

Answer by ldeboer · Dec 01, 2015 at 08:55 PM

You have access to both objects in OnTriggerEnter just check the name

 void OnTriggerEnter (Collider other) {

      // Verify the collider is ObjectB by checking name
      if (other.name == "Whatever Object B is named") {

           // set your bool or whatever you want to do
           yourBoolName = true; 
     }
 }

Assuming you want set the bool false on exit you use OnTriggerExit.

 void OnTriggerExit (Collider other) {

      // Verify the collider is ObjectB by checking name
      if (other.name == "Whatever Object B is named") {

           // Clear your bool or whatever you want to do
           yourBoolName = false; 
     }
 }

Comment
Add comment · Show 2 · 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 robbyisdead · Dec 02, 2015 at 04:22 PM 0
Share

Thanks for the swift response!

In this case, the collider I'm grabbing in other.name is actually the player object's. I want my script to detect when the player has contacted Object B.

avatar image ldeboer robbyisdead · Dec 03, 2015 at 03:05 AM 0
Share

Okay so Object B is an object with the trigger collider and this script. Your player need the bool as a public or write a public function that changes the bool.

So ObjectB will call to ObjectA (player) when triggered ... I will do the public bool method

So this script is on the static objectB and you have a collider and script with a bool on the player. I will call it "PlayerScript" in this code you need to change that to your classname of your script.

  void OnTriggerEnter (Collider other) {
       // Verify the collider is ObjectA (A$$anonymous$$A the player) by checking name
       // You could optionally check the tag if you have multiple players
       if (other.name == "Whatever player is named") {
  
             // Since we know this is our player Fetch the script off the player
             // This will fail and return null if it wasn't a player or script not on
             PlayerScript  temp = other.GetComponent<PlayerScript>();
 
            // set the player script bool directly ... it must be a public
            if (temp != null)  temp.yourBoolName = true;
  
      }
  }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Delete object with key within Trigger 1 Answer

Detect Volume on Collision 1 Answer

C# OnTriggerEnter Issue 3 Answers

How Do I make a 3D Numbered Padlock? 0 Answers

RAW image won't show on UI 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