O
O
OnyxRafe2020-05-02 14:59:20
Unity
OnyxRafe, 2020-05-02 14:59:20

Why is OnTriggerEnter2d not working?

I decided to add the script not to the objects themselves, but to another object that is the parent of these objects, but OnTriggerOnter2D does not work, I even indicated in the condition: if objects with tags touch, then something happens.
But the script works if I add it to the child objects themselves.
OntriggerEnter2D cannot work with foreign objects, but only with those that have a script with this trigger enter?
And yet, for some reason, if you attach a script to child objects, then when interacting with a given collider, the game freezes tightly, but I don’t have infinite loops.

If so, here is the code itself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CathingItems : MonoBehaviour
{
    PlayerMove moving;
  //  public GameObject box;

    public Image hp1;
    public Image hp2;
    public Image hp3;

    public GameObject box1;
    public GameObject box2;
    public GameObject box3;
    public GameObject box4;

    public int score = 0;
    int nightcore = 0;
    public Text coinvalue;
    public int hp = 3;
    public Text hpp;
    void Start()
    {
        //hpp = GetComponent<Text>();
        moving = GameObject.Find("BoxColl").GetComponent<PlayerMove>();
        hp = 3;  
    }
    void getScore()
    {
        coinvalue.text = score.ToString();
       
    }

    void hpCheck()
    {
        if (hp == 0)
        {
            hp1.enabled=false;
            hp2.enabled = false;
            hp3.enabled = false;

        }
        if (hp == 1)
        {
            hp1.enabled = true;
            hp2.enabled = false;
            hp3.enabled = false;
        }
        if (hp == 2)
        {
            hp1.enabled = true;
            hp2.enabled = true;
            hp3.enabled = false;
        }
        if (hp == 3)
        {
            hp1.enabled = true;
            hp2.enabled = true;
            hp3.enabled = true;
        }
    }

    void Update()
    {
        if (hp == 0) Debug.Log("Dead");
        nightcore = score;
        
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        while (true)
        {
            if (collision.gameObject.tag == "shirt" && box1)
            {
                if (score < 0) score = 0;
                score += 1;
                getScore();
                Debug.Log("hi");
            }
            else
            {
                score -= 1;
                if (score < 0) score = 0;
                getScore();
            }
            if (collision.gameObject.tag == "sock" && box2)
            {
                if (score < 0) score = 0;
                score += 1;
                getScore();
            }
            else
            {
                score -= 1;
                if (score < 0) score = 0;
                getScore();
            }
            if (collision.gameObject.tag == "jeans" && box3)
            {
                if (score < 0) score = 0;
                score += 1;
                getScore();
            }
            else
            {
                score -= 1;
                if (score < 0) score = 0;

                getScore();
            }
            if (collision.gameObject.tag == "cap" && box4)
            {
                if (score < 0) score = 0;
                score += 1;

                getScore();
            }
            else
            {
                score -= 1;
                if (score < 0) score = 0;

                getScore();
            }
            if (collision.gameObject.tag == "hpAbi" && gameObject.tag=="boxlayer")
            {
                if (hp >= 3) hp = 3;
                hp++;
                hpCheck();
                hpp.text = hp.ToString();

            }

            if (collision.gameObject.tag == "hpD" && gameObject.tag == "boxlayer")
            {
                if (hp <= 0) hp = 0;
                hp--;
                hpCheck();
                hpp.text = hp.ToString();
            }
           
            if (collision.gameObject.tag == "speedAbi" && gameObject.tag == "boxlayer")
            {
                moving.speed++;
                Debug.Log(moving.speed);
            }
           
            if (collision.gameObject.tag == "speedD" && gameObject.tag == "boxlayer")
            {
                moving.speed--;
                Debug.Log(moving.speed);
            }
            
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OblivionGM, 2020-05-03
@OnyxRafe

OnTriggerEnter2D (just like 3D) only checks the touches of the object on which the script in which it is implemented hangs. If the object on which the script hangs has "children" - they are not covered by the check of touches to triggers. And it hangs tightly because, as already mentioned above, you have an infinite loop, and I advise you to read materials about the while loop, when it should be used and how it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question