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 stay.metal · Feb 10, 2015 at 07:28 AM · triggerboxcollider2dontriggerstay

Ontrigger if

Im trying to make so that when i click my mouse button i want to see if my two Boxcolliders are touching and make my bool true and if true log. The problem is if i click no matter what it keeps printing where am i going wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Thetrigger : MonoBehaviour {
 
     private bool triggered = false;
     public GameObject square1;
     
     void Update(){
         if (Input.GetMouseButtonDown (0)) {
             if (triggered |= true){
                 Debug.Log ("bam");
             }
                 }
             
     }
 
     void OnTriggerStay2D(Collider2D coll){
     if (gameObject.tag == "squarerow"){
                         triggered = true;
         }
         else{
             triggered = false;
         }
         }
 }


Thank you

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IvovdMarel · Feb 10, 2015 at 07:35 AM

Replace the OnTriggerStay method with:

 void OnTriggerEnter2D(Collider2D coll){
      if (gameObject.tag == "squarerow"){
                          triggered = true;
          }
         
 }

 void OnTriggerExit2D(Collider2D coll){
      if (gameObject.tag == "squarerow"){
                          triggered = false;
          }
         
 }

The OnTriggerStay only gets called when the trigger are colliding. Therefore, using the if-else within that method is wrong. The if-else refers to the other object having a tag of the name "squarerow", not to object colliding or not.

EDIT: Also you need to fix the bug that you use gameObject.tag instead of coll.gameObject.tag. In both methods it should be:

  if (coll.gameObject.tag == "squarerow"){
                              triggered = false;
              }

Comment
Add comment · Show 1 · 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
avatar image stay.metal · Feb 11, 2015 at 06:19 AM 0
Share

I used the method you showed me but it still wont detect if colliding

avatar image
0

Answer by tanoshimi · Feb 11, 2015 at 06:39 AM

Your syntax is incorrect. Assuming you meant to print bam when they're not touching:

 if (triggered |= true){

Should be:

 if (triggered != true){
Comment
Add comment · Show 2 · 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
avatar image stay.metal · Feb 11, 2015 at 07:09 AM 0
Share

i want to print bam when they are touching, but it seems as though my if else statement isn't working, i made the bool public to see it was turning on and off but it wasnt.

avatar image IvovdMarel · Feb 11, 2015 at 07:28 AM 0
Share

The other mistake was that you check the wrong gameObject. Please see my edited answer

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerStay still called after moving a parent gameobject 0 Answers

Turning off trigger with multiple box colliders? 0 Answers

How to make OnTriggerEnter2D affect only one collider? 0 Answers

How can I make an enemy hurt the player? 3 Answers

OnTriggerStay simple question 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