在第一個腳本:爲什麼在創建新的GameObjects時不會更改標籤?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstantiateObjects : MonoBehaviour
{
public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
//generateObjectOnTerrain();
}
public void generateObjectOnTerrain(bool parent, string tag)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
if (parent)
objInstance.transform.parent = this.transform;
objInstance.transform.tag = tag;
}
}
而在腳本,我使用這個:
private void Start()
{
for (int i = 0; i < cloneTeleportations; i++)
{
InstantiateObjects gos = GetComponent<InstantiateObjects>();
gos.prefab = prefab;
gos.generateObjectOnTerrain(true, "ddd");//"Teleportation");
}
}
如果我將它從真更改爲false它不會使孩子的GameObjects,如果這是真的,他們會成爲孩子。母公司正在運作。 但對於測試我試圖改變標籤爲「DDD」我在第一個腳本看到,標籤是「DDD」:
objInstance.transform.tag = tag;
標籤爲「DDD」和objInstance.transform.tag默認情況下爲當運行遊戲時,所有被標記爲「Teleportation」而非「ddd」的克隆遊戲對象都是「傳送」。
您是否在Unity中創建了一個標籤'ddd'? – Hristo
啊,對了,忘了吧。 –