C# Unity不破坏带有TypeFruit的OnTiggerInter2D对象

C# Unity不破坏带有TypeFruit的OnTiggerInter2D对象,c#,unity3d,C#,Unity3d,我不熟悉Unity和c#,现在已经学习了几个月了。我对我正在做的一个项目有问题。在游戏中,一名玩家在树上采摘水果。我想当玩家与标签为“TypeFruit”的物体碰撞时,水果被销毁。当玩家触摸水果时,水果没有被采摘,我不知道为什么。有人能分享一下这方面的知识吗?没有显示任何错误消息。多谢各位 我试过这个 void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "TypeFruit")

我不熟悉Unity和c#,现在已经学习了几个月了。我对我正在做的一个项目有问题。在游戏中,一名玩家在树上采摘水果。我想当玩家与标签为“TypeFruit”的物体碰撞时,水果被销毁。当玩家触摸水果时,水果没有被采摘,我不知道为什么。有人能分享一下这方面的知识吗?没有显示任何错误消息。多谢各位

我试过这个

void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "TypeFruit")
        {
            Destroy(collision.gameObject);
        }
    }
这是玩家脚本

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

public class player_movement : MonoBehaviour
{

    int speed=2;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("w"))
        {
            transform.Translate(0, 0.05f * speed, 0);

        }
        if (Input.GetKey("s"))
        {
            transform.Translate(0, -0.05f * speed, 0);


        }
        if (Input.GetKey("a"))
        {
            transform.Translate(-0.05f * speed, 0, 0);
        }
        if (Input.GetKey("d"))
        {
            transform.Translate(0.05f * speed, 0, 0);
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "TypeFruit")
        {
            Destroy(collision.gameObject);
        }
    }

}
这是水果剧本

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

public class Fruit : MonoBehaviour
{
    int Healthiness;



    void Start()
    {




    }

}
这是treeplacer脚本。树置换器在地图上随机生成树。每棵树都结果实。我希望玩家如果碰了水果就去摘。水果被贴上标签“TypeFruit”

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类树置换器:单行为
{
公共雪碧[]水果雪碧=新雪碧[3];
公共果园;
公共游戏对象树prefab;
//在第一帧更新之前调用Start
void Start()
{
int TreeCount=Random.Range(8,16);
对于(int i=1;i currentNumberOfFruits>=maxNumberOfFruits);
}
}
}
枚举果型
{
苹果、香蕉、椰子
}
完全没有错误。树木繁殖得非常好。水果也可以很好的产卵,但是当玩家触摸它时,不会被采摘。

这可能是其中之一 1.在标签名中输入错误
2.其中一个对象没有2D Collider或rigidbody2d

您是否将rigidbody2d附加到水果上?不幸的是,OnTrutGeTele2D工作是必需的。我以前修复了这个bug。你不是水果上的硬汉。不管怎样,谢谢你提到这件事!:)我以前修过这个。非常感谢。问题是2。无论如何谢谢你的帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreePlacer : MonoBehaviour
{
    public Sprite[] FruitSprites = new Sprite[3];
    public Fruit FruitPrefab;

    public GameObject TreePrefab;
    // Start is called before the first frame update
    void Start()
    {
        int TreeCount = Random.Range(8, 16);
        for(int i=1; i<=TreeCount; i++)
        {
          GameObject CurrentTree= Instantiate(TreePrefab, new Vector3(Random.Range(-5, 6), Random.Range(-5, 6), 0), Quaternion.identity);
            CurrentTree.AddComponent(typeof(Tree));
            CurrentTree.GetComponent<Tree>().FruitPrefab = FruitPrefab.gameObject;
            CurrentTree.GetComponent<Tree>().myTreePlacer = this;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    void PlaceTree()
    {

    }

}

public class Tree:MonoBehaviour
{

    public TreePlacer myTreePlacer;
    public GameObject FruitPrefab;
    public int maxNumberOfFruits;
    public int regrowTime;
    public int climbTime;
    int currentNumberOfFruits=0;
    FruitType MyFruitType;




    void Start()
    {

        MyFruitType = (FruitType)Random.Range(0, 3);

        switch (MyFruitType)
        {
            case FruitType.apple:
                maxNumberOfFruits = 5;
                regrowTime = 20;
                climbTime = 5;

                break;

            case FruitType.banana:

                    maxNumberOfFruits = 5;
                        regrowTime = 30;
                    climbTime = 10;
                    break;

            case FruitType.coconut:
                maxNumberOfFruits = 3;
                regrowTime = 60;
                climbTime = 30;
                break;



        }

        StartCoroutine(GrowFruit());




    }

    IEnumerator GrowFruit()
    {

        while (true)
        {

            yield return new WaitForSeconds(regrowTime);

            Fruit CurrentFruit = Instantiate(FruitPrefab, new Vector3(transform.position.x + Random.Range(-0.5f, 0.5f), transform.position.y + Random.Range(-0.5f, 0.5f)), Quaternion.identity).GetComponent<Fruit>();
            CurrentFruit.GetComponent<SpriteRenderer>().sprite = myTreePlacer.FruitSprites[(int)MyFruitType];
            currentNumberOfFruits++;
            yield return new WaitWhile(() => currentNumberOfFruits >= maxNumberOfFruits);
        }
    }



}



enum FruitType
{
    apple, banana, coconut
}