Search Unity

Question OnTriggerEnter2D(Collider2D collision) if and && Question.

Discussion in 'Scripting' started by lku0716, Sep 27, 2022.

  1. lku0716

    lku0716

    Joined:
    Nov 7, 2021
    Posts:
    3
    Hi guys.. I'm from South Korea and i have a really serious question..

    I want to use && for OnTriggerEnter2D but it never works..

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.             if (collision.gameObject.tag == "Oil")
    4.             {
    5.                 result[0].SetActive(true);
    6.                 StartCoroutine(ResultFalse());
    7.                 DataController.Instance.gameDB.energyTycoon.scoreCurrent += 1f;
    8.                 // TycoonDataControl.Instance.scoreCurrent += 1f;
    9.                 Debug.Log("H2 + 목적물질 생성 성공");
    10.             }
    11.  
    12.             else if (collision.gameObject.tag == "ReEnergy" && collision.gameObject.tag == "Water")
    13.             {
    14.                 result[1].SetActive(true);
    15.                 StartCoroutine("ResultFalse");
    16.                 Debug.Log("H2 + Co2 조합 성공");
    17.             }
    18.  
    19.             else if (collision.gameObject.CompareTag("NaturalGas") &&
    20.             collision.gameObject.CompareTag("Water"))
    21.             {
    22.                 result[2].SetActive(true);
    23.                 StartCoroutine("ResultFalse");
    24.                 Debug.Log("H2 + O2 생성 성공");
    25.             }
    26.  
    27.             // else
    28.             // {
    29.             //     result[3].SetActive(true);
    30.             //     StartCoroutine("ResultFalse");
    31.             //     Debug.Log("틀렸지유~!");
    32.             // }
    33.     }
    Code (CSharp):
    1. if (collision.gameObject.tag == "Oil")
    2.             {
    3.                 result[0].SetActive(true);
    4.                 StartCoroutine(ResultFalse());
    5.                 DataController.Instance.gameDB.energyTycoon.scoreCurrent += 1f;
    6.                 // TycoonDataControl.Instance.scoreCurrent += 1f;
    7.                 Debug.Log("H2 + 목적물질 생성 성공");
    8.             }
    this code is woking but under line from this code never work..ㅠㅠ
    Please help me..
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,450
    This isn't a 2D question, it's a scripting question so I'll move your post to the scripting forum.

    This is just basic C# logic.
    Code (CSharp):
    1. collision.gameObject.tag == "ReEnergy" && collision.gameObject.tag == "Water"
    A string cannot be two things at the same time. It cannot be "ReEnergy" AND "Water"!

    I believe you want to check if it's "ReEnergy" OR "Water"? If that's the case then use the logical OR operator is "||".

    https://www.tutorialspoint.com/csharp/csharp_logical_operators.htm

    NOTE: Your post went for moderation because of the use of those characters. The forum system does this because we get a lot of spam so tends to flag it for moderation. Unfortunately this means you should try to avoid posting them if you don't want it to be delayed because of moderation.
     
    mopthrow and TheDevloper like this.
  3. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    to make your condition work, both sentences should be true
    i mean this one collision.gameObject.tag == "ReEnergy"
    and this one collision.gameObject.tag == "Water".

    you could use the or operator
    Code (CSharp):
    1. collision.gameObject.tag == "ReEnergy" || collision.gameObject.tag == "Water"
     
  4. lku0716

    lku0716

    Joined:
    Nov 7, 2021
    Posts:
    3
    Thank you guys.

    I want to print out the case where both strings are true.
    but if i use 'OR', '||' then if either result is true, does the result[1] value become SetActive(false)?
    I would like to print it out if the two string values are true. (ಥ _ ಥ)
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,837
    It's already been illustrated that a game object cannot have more than one tag. Nor can they have more than one name, or be on more than one layer.

    What objects can have multiple of is components. That should lead you in the right direction.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,837
    Okay... My point still stands.

    You're going to have to change how your logic is structured here. Probably by a lot.

    If these things are going in one by one, maintain a collection of the correct, I'm going to say, 'ingredients', if one matches an ingredient in the list, remove it, rinse and repeat until everything has been ticked off.
     
  7. lku0716

    lku0716

    Joined:
    Nov 7, 2021
    Posts:
    3
    Thanks to answer my question.
    Then.. do u have any idea about this logic??
    If you have please tell me, anything is okay