- Home /
OnTriggerStay2D activating all instances
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.
Answer by judahcline · Mar 12, 2021 at 03:54 PM
try setting all variables (float, bool, int, string) to private IE: public bool isTrue; --> private bool isTrue; same with all non update functions IE: void checkSprite() {} --> private void checkSprite() {} also if you are calling a variable or function from outside of the script (the script will have a script variable: public playerMovement playermovement; and then call out stuff like: playermovement.isDead = true;) then it will call all instances of it unless specified so call from within the script rather then outside. (if any of the variables need to be public then find a way to use an if statement through non duplicated scripts)
Your answer
![](https://koobas.hobune.stream/wayback/20220613030550im_/https://answers.unity.com/themes/thub/images/avi.jpg)