This might just be that I’m kind of a bad programmer, but I’m getting very confused. I’m trying to generate a game in which you connect logic gates and try to turn on a lightbulb. But I’m having issues with OnTriggerStay2D. It might be because I’m reutilizing code for all similar gates.
THE ISSUE: Whenever I try to connect two cables to an AND gate, all the AND gates activate at the same time. At first I thought it was an issue with the sprites just changing, but it changes the activation of all AND gates, to the point that you can just connect two switches to the main AND gate and go all the way to the end and connect the last AND gate with the light bulb while not connecting anything else. I already tried using “this” s, but I think the main problem is in the Connections part. Here’s my program.
AND Gate Turning Onn and off:
public class ToggleAND : MonoBehaviour
{
[SerializeField] private Sprite onSprite, offSprite;
private SpriteRenderer spriteRenderer;
public bool isTrue;
public static bool leg1, leg2;
// Start is called before the first frame update
void Start()
{
this.isTrue = false;
this.spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if(leg1 && leg2){
this.isTrue = true;
} else {
this.isTrue = false;
}
checkSprite();
}
void checkSprite(){
if(isTrue){
this.spriteRenderer.sprite = onSprite;
} else {
this.spriteRenderer.sprite = offSprite;
}
}
}
Here’s the Connections code
public class Connection : MonoBehaviour
{
public static bool turnedOn;
void OnTriggerStay2D(Collider2D trig)
{
if(turnedOn){
if(trig.gameObject.CompareTag("Leg1")){
ToggleAND.leg1 = true;
Debug.Log(trig.name + "Touched Leg 1");
} else if (trig.gameObject.CompareTag("Leg2")){
ToggleAND.leg2 = true;
Debug.Log(trig.name + "Touched Leg 2");
} else if (trig.gameObject.CompareTag("LightBulb")){
ToggleBulb.isTurned = true;
Debug.Log(trig.name + "Touched Light Bulb");
}
}
}
void OnTriggerExit2D(Collider2D trig)
{
if(trig.gameObject.CompareTag("Leg1")){
ToggleAND.leg1 = false;
Debug.Log(trig.name + "Leg 1 turned off");
} else if (trig.gameObject.CompareTag("Leg2")){
ToggleAND.leg2 = false;
Debug.Log(trig.name + "Leg 2 turned off");
} else if (trig.gameObject.CompareTag("LightBulb")){
ToggleBulb.isTurned = false;
Debug.Log(trig.name + "LightBulb turned off");
}
}
}
Here’s the ToggleSwitch code:
public class ToggleSwitch : MonoBehaviour
{
public Sprite offSprite;
public Sprite onSprite;
private bool isTurned;
private SpriteRenderer spriteRenderer;
// Start is called before the first frame update
void Start()
{
isTurned = false;
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}
/*
When you click on the light switch, it's gonna either turn on or off,
depending on the previous state, and it's gonna change the sprite to
show the change.
*/
void OnMouseDown()
{
if(Input.GetMouseButtonDown(0)){
if(isTurned){
isTurned = false;
Debug.Log(isTurned);
Connection.turnedOn = false;
spriteRenderer.sprite = offSprite;
} else {
isTurned = true;
Debug.Log(isTurned);
Connection.turnedOn = true;
spriteRenderer.sprite = onSprite;
}
}
}
}
If anybody can help me, I’ll truly appreciate it. If you need any more code, I can always send more.