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
0
Question by sporkflips · Nov 25, 2016 at 11:08 AM · c#colliderstriggers

Detect which trigger a collider is inside instead of which collider is inside a trigger

I've created scripts that spawn a grid of soil sections and sectors (sectors containing sections) and now I want another GameObject (a plant) to be able to detect which section and sector it is inside.

basically I want to do something like this, in theory

    PlantClass { 
         GameObject _soilSection;
     
         OnTriggerStay(Trigger other)   //instead of Collider other, so the plant can detect which trigger it is inside instead of the trigger detecting which collider is inside it
             {
                 if (other.gameObject.tag == "SoilSection")
                     {
                         _soilSection = other.gameObject;
                     }
             }
     }

Is something like this possible without having every soil section regularly update to some trigger manager that would be accessed by the plant?

I don't think it would make sense to do it with a trigger manager because there are a lot of soil sections and only one of them will have the plant in question inside it.

Comment
Add comment · Show 3
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 metalted · Nov 25, 2016 at 09:49 PM 1
Share

I think this might be possible using a simple script. Try using OnTriggerEnter(Collider : collider){...}, using something like Send$$anonymous$$essage() to send the trigger id to the collider that's in the trigger, in this case your plant.

Because its a grid, you probably place them using a pair of for loops. That way you can make a section prefab and pass the id while creating the grid. The prefab will contain the id and a function to send the info, or you can store this function in a super class of course.

I don't think its a good idea to use OnTriggerStay, because this will call every frame once theres a plant inside. Don't know if this will help you, but this is the way I would fix it I think. I used something similar when I was recreating a battleship game.

avatar image sporkflips metalted · Nov 26, 2016 at 12:15 AM 0
Share

That actually does seem like it would make more sense, since there would be a lot of plants and they'd all be calling OnTriggerStay almost every frame. Thanks for your reply.

So is OnTriggerEnter called even if a GameObject with a collider is instantiated inside the bounds of the trigger? as opposed to only being called if something passes through the edge of the collider/trigger, entering from outside?

I think an issue that would come up with trying to pass the section id when the grid is created is that plant objects will be created and destroyed throughout the grid after it is created, so they'd still have to be able to detect which section they're in when they're created. I guess I could just have the SoilClass use OnTriggerEnter to pass its id to whatever enters it at the time it's created?

the way the grid is created is from a starter prefab with a pair of for loops though. I'm unfortunately not familiar with super classes yet. I guess I am fairly new to program$$anonymous$$g.

avatar image sporkflips metalted · Nov 26, 2016 at 09:51 AM 1
Share

I think I'm going to try to just do something like this inside the SoilClass

 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Plant")
         {
             other.GetComponent<Plant>().soilSection = gameObject;
         }
     }

1 Reply

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

Answer by ElijahShadbolt · Nov 25, 2016 at 10:09 PM

It already works that way. Add this script to your Plant GameObject to see the effects.

ExampleClass.cs

 using UnityEngine;
 
 public class ExampleClass : MonoBehaviour
 {
     void OnTriggerEnter(Collider other)
     {
         Debug.Log(name + " is entering Trigger " + other.name);
     }
 
     void OnTriggerStay(Collider other)
     {
         Debug.Log(name + " is within Trigger " + other.name);
     }
 
     void OnTriggerExit(Collider other)
     {
         Debug.Log(name + " is leaving Trigger " + other.name);
     }
 }
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

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

247 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 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 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 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 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 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

Having problems with playing a sound (C#) 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I make a collider ignore only one other collider? 0 Answers

OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 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