- Home /
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.
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.
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.
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;
}
}
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);
}
}