- Home /
Looking for a solution as to why my collider array isn't registering any objects.
I'll see what I can do to explain this in code and screenshots. I have a prefab mesh which I'm after some code on.
That hexagonal tile you can see is the one: the CircuitHex. You'll notice there are a few others too, flush fit together. That script that it has includes a function call which I hopefully want to get working.
I want an array to be made, which identifies the hexe's neighboring tiles, i.e the six possible spaces a tile can fit in next to it.
Currently it's doing it like so.
public class Hex : MonoBehaviour {
public virtual void RegisterHexes()
{
Collider[] adjacentHexes = Physics.OverlapSphere(position: gameObject.transform.position, radius: 86.0f, layerMask:8);
foreach (Collider hex in adjacentHexes)
{
print("Neighbor Found");
}
}
}
That's a parent class by the way, and I had a look around at various ways of detecting the objects, opting for a sphere check. If you know of any better ways of checking for the surrounding hexes do tell me.
That RegisterHexes should, unless I've got my syntax muddled, print out a message for each of the objects that the sphere check picks up (Should print twice for this hex, since there are two next to it.)
And this function is currently being called in the child class, inside a function of its own, called Activated, which I've just set to fire on a standard mouse click on the object.
public class CircuitHex : Hex
{
void Activated()
{
RegisterHexes();
if (OnActivation != null)
{
OnActivation();
}
}
}
Unfortunately this code isn't working properly. I checked the debug and RegisterHexes is indeed being called when I click the hex, but no message is being printed, meaning no neiboring hexes are being found. The radius and layermask should be fine. I have a sneaking suspicion it's because there's no gameobject attached to the parent class, since the CircuitHex child script is the one attached to the gameobject.
Is there a way to get around this problem? Ideally I don't want to move that check function down into the child, since other scripts will need it, and I'd sooner pass it to them through inheritance than using an instance or a delegate.
Any help is greatly appreciated. Hope my question is understandable.
Was hoping to avoid physics where I could, since these tiles won't be moving, except through a coroutine. The Rigidbody doesn't seem to have changed anything unfortunately.
Your answer
Follow this Question
Related Questions
Removing Vector3 Decimal for Position 2 Answers
does not work check if dead 1 Answer
Help around an approach to a "Virtual Motion Table" in Unity 0 Answers
Instantiated prefab position 5 Answers
Setting Position of Spawned Prefab 2 Answers