' UnityEngine.GameObject []'不包含`Transform'的定义错误

时间:2016-08-26 15:09:05

标签: c# unity3d unityscript

我正在编写一个脚本来生成我的播放器运行的路径,就像一个无尽的跑步者。我试图访问我自己的"转换(播放器的转换)并将其置于playertransform变量中,但会出现此错误。下面是完整的代码: -

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

public class tilemanager : MonoBehaviour {

    public GameObject[] tileprefabs;

    private Transform playertransform;
    private float spawnz = 0.0f;
    private float tilelength = 16.0f;
    private int amountoftilesonscreen = 7;
    private List<GameObject> activetiles;
    private float safezone = 15.0f;

    void Start(){

        activetiles = new List<GameObject>();
        playertransform = GameObject.FindGameObjectsWithTag("Player").Transform;

        for(int i = 0; i < amountoftilesonscreen; i++){

            SpawnTile();
        }
    }

    void Update(){

        if(playertransform.position.z - safezone > (spawnz - amountoftilesonscreen * tilelength)){

            SpawnTile();
            DeleteTile();
        }
    }

    void SpawnTile(int prefabIndex = -1){

        GameObject go;

        go = Instantiate(tileprefabs[0]) as GameObject;
        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * spawnz;
        spawnz += tilelength;
        activetiles.Add(go);
    }

    void DeleteTile(){

        Destroy(activetiles[0]);
        activetiles.RemoveAt(0);
    }
}

3 个答案:

答案 0 :(得分:1)

因为GameObject.FindGameObjectsWithTag返回一个gameobject数组。改为使用GameObject.FindWithTag。

答案 1 :(得分:0)

您正在使用返回X数组的FindGameObjectsWithTag()。将其更改为gameObject

正确的代码是:

FindWithTag()

答案 2 :(得分:0)

如果脚本是您要查找的对象的组成部分,您可以仅将其引用为 echo %JAVA_HOME%

transform

,如果您想将其包含在变量中,请使用:

transform.position;