我试图在unity3d中克隆对象,但却遇到两个错误。

时间:2017-02-18 03:38:11

标签: c# unity3d unity5

目标是使用此脚本随机克隆游戏对象:地形区域中的地点,高度和大小。

在场景的编辑器中,我添加了一个新的空游戏对象并将其重命名为:ObjectToClone

然后我添加了两个新标签:Terrain和ObjectToClone 使用标记ObjectToClone标记ObjectToClone并使用标记Terrain标记Terrain。

将脚本拖到ObjectToClone游戏对象中并在脚本中的Inspector中添加了ObjectToClone

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class CloneObjects : MonoBehaviour
{

    public GameObject ObjectToClone;
    public int objectsStartingHeight = 20;
    [HideInInspector]
    public GameObject[] clonedObjects;

    // for tracking properties change
    private Vector3 _extents;
    private int _objectCount;
    private float _objectSize;
    private List<int> randomNumbers = new List<int>();

    /// <summary>
    ///     How far to place spheres randomly.
    /// </summary>
    public Vector3 Extents;

    /// <summary>
    ///     How many spheres wanted.
    /// </summary>
    public int ObjectCount;
    public float ObjectSize;

    // Use this for initialization
    void Start()
    {

        rndNumbers();
        Clone();
        clonedObjects = GameObject.FindGameObjectsWithTag("ObjectToClone");
    }

    private void OnValidate()
    {
        // prevent wrong values to be entered
        Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
        ObjectCount = Mathf.Max(0, ObjectCount);
        ObjectSize = Mathf.Max(0.0f, ObjectSize);
    }

    private void Reset()
    {
        Extents = new Vector3(250.0f, 20.0f, 250.0f);
        ObjectCount = 100;
        ObjectSize = 20.0f;
    }

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

    }

    private void Clone()
    {
        if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
            return;

        // cleanup
        var ShipsToDestroy = GameObject.FindGameObjectsWithTag("ObjectToClone");
        foreach (var t in ShipsToDestroy)
        {
            if (Application.isEditor)
            {
                DestroyImmediate(t);
            }
            else
            {
                Destroy(t);
            }
        }

        var withTag = GameObject.FindWithTag("Terrain");
        if (withTag == null)
            throw new InvalidOperationException("Terrain not found");

        for (var i = 0; i < ObjectCount; i++)
        {
            var o = Instantiate(ObjectToClone);
            o.tag = "Cube";
            o.transform.SetParent(base.gameObject.transform);
            o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);

            // get random position
            var x = Random.Range(-Extents.x, Extents.x);
            var y = Extents.y; // sphere altitude relative to terrain below
            var z = Random.Range(-Extents.z, Extents.z);

            // now send a ray down terrain to adjust Y according terrain below
            var height = 10000.0f; // should be higher than highest terrain altitude
            var origin = new Vector3(x, height, z);
            var ray = new Ray(origin, Vector3.down);
            RaycastHit hit;
            var maxDistance = 20000.0f;
            var nameToLayer = LayerMask.NameToLayer("Terrain");
            var layerMask = 1 << nameToLayer;
            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                var distance = hit.distance;
                y = height - distance + y; // adjust
            }
            else
            {
                Debug.LogWarning("Terrain not hit, using default height !");
            }

            //o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
            // place !
            o.transform.position = new Vector3(x, y + objectsStartingHeight, z);
        }

        _extents = Extents;
        _objectCount = ObjectCount;
        _objectSize = ObjectSize;
    }

    public void rndNumbers()
    {

    }
}

我得到第一个错误

表达式断言失败:&#39; m_InstanceID!= InstanceID_None&#39; 0x000000014151284B(Unity)StackWalker :: GetCurrentCallstack

MissingReferenceException:类型&#39; GameObject&#39;的对象已被破坏,但你仍然试图访问它。 您的脚本应该检查它是否为null或者您不应该销毁该对象。

如果我没有销毁它,即使我设置了许多要克隆的对象,它也会创建很多对象。

0 个答案:

没有答案