首页 > 解决方案 > How can I fix a warning saying something is missing in instantiate?

问题描述

In unity I had started making a game and had a similar problem to another post .How can I make it so that obstacles spawn based on how far the player has moved?. The answer on this had helped me very much and had used it on my code. I got another problem now as now I need to destroy my obstacles and I followed a similar concept to the code in the post. But now it says

The referenced script on this Behaviour (Game Object 'Triangles') is missing! UnityEngine.Object:Instantiate(GameObject) TriangleSpawner:Update() (at Assets/Scripts/TriangleSpawner.cs:22)

This is my code

if (Mathf.Abs(transform.position.x - Location) >= 15)
        {
            Location = transform.position.x;
            GameObject newTriangle = Instantiate(Triangles);
            newTriangle.name = Triangles.name;
            newTriangle.transform.position = transform.position + new Vector3(Spacing, Random.Range(-2, 2), 0);
            if (Mathf.Abs(newTriangle.transform.position.x - Player.transform.position.x) < -15)
            {
                Destroy(Triangles);
            }

how can I fix this error?

标签: c#unity3d

解决方案


您正在破坏原始对象而不是克隆。尝试更改Destroy(Triangles);Destroy(newTriangle);


推荐阅读