Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Trouble getting scores for one team only

Discussion in 'Editor & General Support' started by Blitz54, Jan 3, 2021.

  1. Blitz54

    Blitz54

    Joined:
    Dec 20, 2020
    Posts:
    20
    Hello! I just recently picked up Unity (as in 3 days ago) and I've made decent progress on my game, but I'm at a loss now. Figure I'd try my luck at a forum post before I go to bed.

    I'm making a hockey game. Currently what happens, is that when the Puck crosses a goal line, it updates both home and away scores. I can't find a way to select a specific trigger collider. On Trigger Enter seems to be my only option, and just makes the puck react to ANY trigger. What am I missing here? Also, feel free to suggest any trimming on this if you notice something that can be done better.

    Thanks!

    code question.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,930
    Check over in the visual scripting forum on this same board... those folks would be more familiar with the context of what you're doing.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,925
    OnTriggerEnter has a Collider parameter (it's right there in your screenshot). This tells you which collider you collided with. You need to inspect that collider and do some kind of check against it. Perhaps check if it is equal to one goal collider or another. or maybe attach another component to the goals, and go from that collider to that component (using GetComponent) and read which goal it is from there.
     
  4. Blitz54

    Blitz54

    Joined:
    Dec 20, 2020
    Posts:
    20
    Thanks for the advice, I couldn't figure out how to make GetComponent work for me, but I figured another way. I made a scene variable for each net, which let me target the Box Collider Trigger itself. Then I used Get Scene Variable, and ran it and the collider parameter from the On Trigger Event to an Equal check. The On Trigger Event automatically grabs the trigger collider of the net it's attached to. So when compared, it's the same as the variable I made. Then it simply goes through a True/False. Currently I have two versions of this "script". One for each net. But I could probably shorten it by having True score for home, and False score for away.

    Thanks peeps! I'll post this kind of question in scripting next time. I didn't see the VISUAL scripting forum for some reason, just regular scripting and I wasn't sure if this counted. My bad.

    code question 2.png
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,925
    Glad you got it working. You should definitely be able to get this to work with a single script for both goals somehow (a per-instance variable of some kind?). I don't know much about visual scripting though. In a normal Unity MonoBehaviour script I would just have a property on the script called "OwnedByTeam" which designates which team the goal belongs to. Then you can set the value in the inspector for that script. Should be able to do something similar in Visual Scripting but i'm not sure how.
     
    Blitz54 likes this.