Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Having trouble with else if statement inside OnTriggerStay2D

Discussion in 'Scripting' started by afromanzack, Aug 13, 2023.

  1. afromanzack

    afromanzack

    Joined:
    Feb 2, 2021
    Posts:
    9

    No idea why the else if statement inside of OnTriggerStay2d is not triggering when the distance reaches < 10
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Debug it. Debug the distance value and tag yourself or attach a debugger. Nobody on a forum is going to be able to debug it for you.

    Can you see that you've typed "ENemy" and "Enemy" in the same bit of code? This simply looks like you need to pay more attention to detail.

    FYI: This is how to post code on the forum: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. afromanzack

    afromanzack

    Joined:
    Feb 2, 2021
    Posts:
    9
    upload_2023-8-13_13-3-53.png

    kind of new to this forum don't really know what I'm doing but I made it simpler.
    The only Ontrigger2d that is executing is the onTriggerEnter2D.


    and I spelled "ENemy" like that on purpose.
     
  4. afromanzack

    afromanzack

    Joined:
    Feb 2, 2021
    Posts:
    9
    Distance is not being calculated and the fighting variable are returning false.
     
  5. afromanzack

    afromanzack

    Joined:
    Feb 2, 2021
    Posts:
    9
    any sort of hint would be emaculate
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    You'll notice when typing, that there is a bar above where you type. In it is things like make text bold, italic, change colors, hyperlinks, bullet points, number points, emojiis, add image, video, and then there is a Code: section. The first code icon is for pasting large scripts in, the rectangle with cursor is for posting small one-liners.

    Copy your code in your code editor, hit the first code button, a window will pop up, paste your code in it, and hit "OK"
     
  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    The tag is a string, it looks like you have added parens or braces unnecessarily I can't tell from a screen capture (that's why you should post the code in code tags.

    Other than that the way you would "know" the distance and the value of fighting is by using Debug.Log to show that information. Don't infer it. And log the actual tag also you could be surprised what it might contain.