C# I';我试图在unity3d中克隆对象,但出现两个错误。

C# I';我试图在unity3d中克隆对象,但出现两个错误。,c#,unity3d,unity5,C#,Unity3d,Unity5,目标是使用此脚本随机克隆游戏对象:地形区域中的位置、高度和大小 在场景编辑器中,我添加了一个新的空游戏对象,并将其重命名为:ObjectToClone 然后我添加了两个新标签:Terrain和ObjectToClone 使用标记ObjectToClone标记ObjectToClone,使用标记Terrain标记Terrain 将脚本拖动到ObjectToClone游戏对象,并在脚本中的检查器中添加ObjectToClone using System; using UnityEngine; usi

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

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

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

将脚本拖动到ObjectToClone游戏对象,并在脚本中的检查器中添加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()
    {

    }
}
使用系统;
使用UnityEngine;
使用Random=UnityEngine.Random;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
公共类克隆对象:单一行为
{
公共游戏对象对象克隆;
public int objectsStartingHeight=20;
[hideininstecpt]
公共游戏对象[]克隆对象;
//用于跟踪属性更改
私有向量3_范围;
私有int_objectCount;
私有浮动对象大小;
私有列表随机数=新列表();
/// 
///随机放置球体的距离。
/// 
公共向量3范围;
/// 
///需要多少个球体。
/// 
公共int对象计数;
公众持股比例;
//用于初始化
void Start()
{
rndNumbers();
克隆();
clonedObjects=GameObject.FindGameObjectsWithTag(“ObjectToClone”);
}
私有void OnValidate()
{
//防止输入错误的值
区段=新矢量3(Mathf.Max(0.0f,区段.x)、Mathf.Max(0.0f,区段.y)、Mathf.Max(0.0f,区段.z));
ObjectCount=Mathf.Max(0,ObjectCount);
ObjectSize=Mathf.Max(0.0f,ObjectSize);
}
私有无效重置()
{
范围=新矢量3(250.0f、20.0f、250.0f);
ObjectCount=100;
ObjectSize=20.0f;
}
//每帧调用一次更新
无效更新()
{
}
私有void Clone()
{
if(extends==\u extends&&ObjectCount==\u ObjectCount&&Mathf.近似值(ObjectSize,\u ObjectSize))
返回;
//清理
var ShipsToDestroy=GameObject.FindGameObjectsWithTag(“ObjectToClone”);
foreach(在ShipsToDestroy中的var t)
{
if(Application.isEditor)
{
立即终止(t);
}
其他的
{
销毁(t);
}
}
var withTag=GameObject.FindWithTag(“地形”);
如果(withTag==null)
抛出新的无效操作异常(“未找到地形”);
对于(var i=0;ivar layerMask=1很明显,在实例化游戏对象之前,您正在销毁该游戏对象。您确定要实例化一个预置吗?似乎您正在尝试实例化一个游戏对象