Hello
I want to be able to make a variable which stores objects with a certain script attached to them which overlaps with the object that this script is attached to. This is so I can find out if a bool variable in one of those objects are set to true in which case a bool variable will be set to true on the object this script is attached to.
The context is that I need to find out if this object is overlapping a wire with an electric current running through it so that this object will become charged and can pass that charge on to other wires without an electric current to then open gates and such.
The way I’m checking if this object is overlapping something is by using “Physics2D.OverlapBox” where i then make it check the objects positon and scale to see if a wire is overlapping it. But I can’t for the life of me figure out how to then be able to store that object in a variable to I can use it in a if statement. IDK maybe it isn’t possible while using OverlapBox but I can’t find anything which seems to fit my specific case so if anyone has some help to offer I’d highly appreciate it.
Your post is a little hard to digest as you didn’t use explicit names for your objects, but unless I’m missing something, you can store the reference either directly or in a list. Something like this?
public class Tester : MonoBehaviour
{
private Machine _machine;
// or: private List<Machine> _machines = new List<Machine>();
private void OnWhateverEventCausesTheCheck()
{
Collider2D collider = Physics2D.OverlapBox( ... );
if(collider != null && collider.TryGetComponent(out Machine machine) == true)
{
_machine = machine;
// or:
// _machines.Add(machine);
}
}
private void Update()
{
if(_machine != null && _machine.someBoolYouWant == true)
{
// do whatever
}
// or:
// foreach(Machine machine in _machines) { // do whatever }
}
}
If you elaborate further, we’ll be happy to help.
1 Like
Thank you so much for your help, I implemented your code and changed it up a bit to get this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Conductor : MonoBehaviour
{
private List<ElectricCurrent> _ElectricCurrent = new List<ElectricCurrent>();
public LayerMask wires;
public bool electricCharge;
private void OnTriggerEnter2D()
{
Collider2D collider = Physics2D.OverlapBox(transform.position, transform.localScale, 0, wires);
if (collider != null && collider.TryGetComponent(out ElectricCurrent wire) == true)
_ElectricCurrent.Add(wire);
}
private void OnTriggerExit2D()
{
Collider2D collider = Physics2D.OverlapBox(transform.position, transform.localScale, 0, wires);
if (collider == null)
{
_ElectricCurrent.Clear();
electricCharge = false;
}
else if (collider != null && collider.TryGetComponent(out ElectricCurrent wire) == true)
{
_ElectricCurrent.Clear();
_ElectricCurrent.Add(wire);
electricCharge = false;
}
}
private void Update()
{
foreach (ElectricCurrent wire in _ElectricCurrent)
{
GetComponent<ElectricCurrent>();
if (wire.electricCurrent == true)
electricCharge = true;
}
}
}
I tested it and it worked almost perfectly! The only problem I run into is that when the object this script is attached to is colliding with several triggers at once it only stores 1 of the objects several times instead of every object once. I think I could theoretically impliment a bool variable to see if an object is already stored but I’m a bit unsure as to how I’d go about doing that, nor do I even know if it’s the best way to do it. For all I know Unity might have a built in feature to help out.
I’m trying to fix it by myself but I thought that it wouldn’t hurt to ask in case you’d know how to help. But even if not I want to thank you kindly for the help already provided, I had really tunnelminded myself into a certain solution and this solution was completly different.
(Note: The ElectricCurrent script only contains 1 bool variable “electricCurrent” which is meant to signify if an object which its attached to has an electric current or not)[/QUOTE]
Your OnTriggerEnter2D (and Exit) don’t have the correct signature, and I’m a little confused on the OverlapBox call inside of them, as the (real) callbacks will give you the correct information. If you want to find more than one collider in an overlap check, you need to use OverlapBoxAll instead.
As for tracking multiple instances, you can use List.Contains to check if the object is already in the list before adding it again and guarantee uniqueness.