Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by QuinMo22 · Mar 12, 2021 at 12:07 PM · programmingtriggersinstances

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Trying to make simple trigger score counter, having troubles. 0 Answers

OnTriggerEnter/OnCollisionEnter and 5.0 3 Answers

Make something arrive at position in exactly X seconds? 2 Answers

Changing Enemy Prefab Variable (MoveSpeed) at Runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges