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