Unity3D给出脚本错误:编译时出现OnTriggerEnter

时间:2019-08-06 11:15:10

标签: c# unity3d triggers collision

我的代码具有MonoBehaviour;

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name);
    }

当我编译这段代码时,我得到这个错误:

Script error: OnTriggerEnter
This message parameter has to be of type: Collider
The message will be ignored.

即使在任何地方都没有使用MonoBehaviour,也是如此。 (通过代码添加)

添加代码的代码:

    private void SpawnItem(Vector3Int pos)
    {
        new GameObject().AddComponent<ItemGameobject>().Initialize(pos, new Item(GetBlockTypeAt(pos), 1));
    }

整个MonoBehavior供参考:

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

public class ItemGameobject : MonoBehaviour
{
    private Item item;
    private World world;

    private MeshRenderer rendere;
    private MeshFilter filter;
    private Material mat;
    private Mesh mesh;
    private List<Vector3> verts = new List<Vector3>();
    private List<int> tries = new List<int>();
    private List<Vector2> uvs = new List<Vector2>();
    private int vertexIndex = 0;

    public void Initialize(Vector3 pos, Item item)
    {
        world = World.Instance;
        this.item = item;

        transform.position = pos + new Vector3(0.5f, 0.5f, 0.5f);
        transform.parent = transform;
        name = "Item";
        gameObject.layer = 9;

        mat = world.Mat;
        gameObject.AddComponent<CollisionObject>();
        gameObject.AddComponent<Rigidbody>();

        var col = gameObject.AddComponent<BoxCollider>();
        col.size = new Vector3(0.25f, 0.25f, 0.25f);
        col.material = world.PhysicsMat;

        col = gameObject.AddComponent<BoxCollider>();
        col.size = new Vector3(0.5f, 0.5f, 0.5f);
        col.isTrigger = true;

        rendere = gameObject.AddComponent<MeshRenderer>();
        filter = gameObject.AddComponent<MeshFilter>();
        mesh = new Mesh();

        DrawVoxel();
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name);
    }

    public Item GetItem()
    {
        return item;
    }

    private void DrawVoxel()
    {
        for (int face = 0; face < 6; face++)
        {
            AddVerticies(face);
            AddTriangles();
            AddTexture(world.BlockTypes[item.BlockType].GetFaceTexture((Face)face));

            vertexIndex += 4;
        }

        mesh.SetVertices(verts);
        mesh.SetTriangles(tries.ToArray(), 0);
        mesh.uv = uvs.ToArray();

        mesh.RecalculateNormals();
        filter.mesh = mesh;
        rendere.material = mat;
    }

    private void AddVerticies(int face)
    {
        verts.Add((BlockData.Vertices[BlockData.Triangles[face, 0]] + new Vector3(-0.5f, -0.5f, -0.5f)) * 0.25f);
        verts.Add((BlockData.Vertices[BlockData.Triangles[face, 1]] + new Vector3(-0.5f, -0.5f, -0.5f)) * 0.25f);
        verts.Add((BlockData.Vertices[BlockData.Triangles[face, 2]] + new Vector3(-0.5f, -0.5f, -0.5f)) * 0.25f);
        verts.Add((BlockData.Vertices[BlockData.Triangles[face, 3]] + new Vector3(-0.5f, -0.5f, -0.5f)) * 0.25f);
    }

    private void AddTriangles()
    {
        tries.Add(vertexIndex + 0);
        tries.Add(vertexIndex + 1);
        tries.Add(vertexIndex + 2);
        tries.Add(vertexIndex + 2);
        tries.Add(vertexIndex + 1);
        tries.Add(vertexIndex + 3);
    }

    private void AddTexture(int atlasIndex)
    {
        float y = atlasIndex / BlockData.TextureAtlasBlockWidth;
        float x = atlasIndex - y * BlockData.TextureAtlasBlockWidth;

        x *= BlockData.NormalizedTextureWidth;
        y *= BlockData.NormalizedTextureWidth;
        y = 1f - y - BlockData.NormalizedTextureWidth;

        uvs.Add(new Vector2(x, y));
        uvs.Add(new Vector2(x, y + BlockData.NormalizedTextureWidth));
        uvs.Add(new Vector2(x + BlockData.NormalizedTextureWidth, y));
        uvs.Add(new Vector2(x + BlockData.NormalizedTextureWidth, y + BlockData.NormalizedTextureWidth));
    }
}

All scripts in the project

1 个答案:

答案 0 :(得分:1)

您可能已将脚本命名为“对撞机”。因此,您只需要重命名脚本即可,因为它与Unity基本类型“ Collider”冲突。