need help with figuring this code out

I been stuck on this coding process for a while now. right now I spawn arrows in each direction (Left right up and down) and I got baskets to direct them to. what I wanted to make is a score/money system to when an arrow is in the right rotation and is directed to the right basket (example, an arrow that points upwards needs to go to the basket up) you get 10 points every right arrow and 5 for a wrong solution. the first code I made, made it so each basket has its own scoring system means they started 0 each. I tried a “game master” object, but it doesn’t seem to work (pictures bellow) I’ll love to know how what I want to do can be done.

Screenshots of code are not helpful. If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

To understand your issue further, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

okay ill tag it here as you might see my main problem now is the empty object the script is attached to cannot seem to use the basket’s colliders since a log never came off, I tried a different input for the upwards one to see if it might notice it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Text txt;
    private int score;
    public Collider2D basketU;
    public Collider2D basketD;
    public Collider2D basketL;
    public Collider2D basketR;


    void Update()
    {
        basketU = gameObject.transform.Find("basketU").GetComponent<Collider2D>();
        basketD = gameObject.transform.Find("basketD").GetComponent<Collider2D>();
        basketL = gameObject.transform.Find("basketL").GetComponent<Collider2D>();
        basketR = gameObject.transform.Find("basketR").GetComponent<Collider2D>();
        txt.text = "score: " + score;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("arrow in");
        Debug.Log(collision.gameObject.name);
        Debug.Log(score);
        if (collision.transform.name == "arrow U(Clone)" && basketU)
        {
            score += 10;
        }
        if (collision.transform.name == "arrow D(Clone)" && collision == basketD)
        {
            score += 10;
        }
        if (collision.transform.name == "arrow L(Clone)" && collision == basketL)
        {
            score += 10;
        }
        if (collision.transform.name == "arrow R(Clone)" && collision == basketR)
        {
            score += 10;
        }
        else
        {
            score += 5;
        }
    }


}

Why are you doing all that work searching for colliders in the current Transform hierarchy each frame? Could you not simply just do that once during start or better still, just drag those colliders into the public properties in the Inspector?

Also, why would “OnTriggerEnter2D” be called on this script? Is there a trigger collider on this “score” object? You’ve only said “but it doesn’t seem to work” which could mean anything. Maybe you mean you never see “arrow in”?

It’s really bad to rely on names like this, especially adding “(Clone)” in. It’s just too fragile. In the very least you should look up how to use tags and move away from comparing names.

“&& basketU” looks wrong when comparing to the other stuff.

As you can see, you’re posting code as if it fully describes the set-up (this is very common) but most of the time it’s only half the story.

1 Like

there are already pictures on the first post that shows that the object “score” is attached to is an empty one, I set all the baskets as it’s children, as for the naming arrows, if I had done it by tagging the arrow’s as “arrow” it mostly mean the baskets would (or in my case currently…wouldn’t) notice each arrow as correct solution…?
I mean checking for their rotation could work too? I am not sure I am new to all this forum thing and I’m not an expert on unity coding knowledge.

Right and that’s out of play mode so there’s no reason to keep updating it per-frame then.

See how you didn’t confirm any important information such as if you’re getting the debug output I mentioned? You said “since a log never came off” but I’m trying to confirm what you mean by this. It’s really essential to understanding things like what are these arrows? Are there colliders on them? I don’t see any detail about them.

Asking lots of questions can be exhausting so it’s always best to provide full information on things that interact rather than focusing on a code dump. Also describe what you expect and what actually happens in detail. If there’s code then say which part doesn’t work. Without your project in front of us, it’s the only way we can help you.

1 Like

hmm no, nothing no logs nor score going up, and forgot to mention the empty game object itself doesn’t have the trigger colliders, the trigger colliders used are childs in it

Again, this is vague. You’re describing something you know about, not us. “Empty Game Object” could mean anything.

Physics is about different colliders interacting. If you’ve got a trigger, it won’t do anything unless another collider enters it. Physics doesn’t look at sprites because why would it? If you believe that then it seems you might have skipped the basic physics tutorials so it might be worth you checking those out, there are lots of them.

I say the above because it now sounds like you’re expecting an arrow sprite to somehow cause a trigger callback? If you believe that then what collider2D do you think the callback is passing you? Sounds like you maybe expect it to be the arrow collider but you’ve not added one? I am of course guessing here.

the arrows have a normal collider (non trigger), the object called “GameMaster” has 4 “baskets” attached to it, the baskets have 2 sets of colliders, ones which are longer and are trigger and smaller ones which are non trigger, which are for the arrows to “bump” into and get destroyed.
what I expect to happen is: that the object “GameMaster” would notice when arrows are inside the Trigger areas of the baskets that are attached to them, and give score properly

Does anything have a Rigidbody2D on it? Things that move should do of course. If not then all these colliders are Static (non-moving). Static do not contact Static because there’d be no need, they never move.

If they do then the only other reason would be that the Layers these colliders are on are not set to collide in the Layer Collision Matrix in Project Settings > 2D Physics.

the arrows and baskets both do, baskets with kinematic ones, arrows with dynamic although I doubt the baskets really do need the rigid bodies :eyes:

So it’ll either be because of the Layer Collision Matrix above or that the GameObject the “Score” component is on, isn’t where the Rigidbody2D is.

There isn’t anything else. Physics is simple and by default everything contacts everything else. For the callback to happen you need to spell the callback name correctly and place it on either the Rigidbody2D or Collider2D that’s hitting stuff.

that was my first problem, placing score script on the baskets set the score to go up, but start on 0 on each side of basket… which is not the imply. only solution I can think of is to make the four of the baskets as one or to make the triggers on the Gamemaster

seem like my second solution works, placing 4 triggers on my “GameMaster” and placing them on the public tabs on the inspector does the job