我有生成障礙順序的代碼:
using UnityEngine;
using System.Collections.Generic;
public class GroundSpawner : MonoBehaviour
{
public GameObject groundTile;
public static string item;
private List<string> listOfChoices = new List<string>{"box", "antitank", "barricade", "wheels"};
private List<string> roadList = new List<string>();
Vector3 nextSpawnPoint;
void Start()
{
roadList = GenItemList(10, 2);
for (int i = 0; i < roadList.Count; i )
{
item = roadList[i];
GameObject temp = Instantiate(groundTile, nextSpawnPoint, Quaternion.identity);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
}
}
public List<string> GenItemList(int numOfItems, int numOfTanks)
{
List<string> returnList = new List<string>();
for (int i = 0; i < numOfItems; i )
{
int randomIndex = Random.Range(0, listOfChoices.Count);
string add = listOfChoices[randomIndex];
returnList.Add(add);
}
for (int i = 0; i < numOfTanks; i )
{
int randomIndex = Random.Range(1, numOfItems);
returnList[randomIndex] = "tank";
int tankIndex = randomIndex;
int numOfMolotoves = Random.Range(1, 4);
for (int j = 0; j < numOfMolotoves; j )
{
randomIndex = Random.Range(0, (tankIndex - 1));
if(returnList[randomIndex] != "molotov")
{
returnList[randomIndex] = "molotov";
}
else
{
numOfMolotoves ;
}
}
}
return returnList;
}
}
在void Start()
里面有一行:
item = roadList[i];
這條線是我想要生成的障礙物(盒子、路障、輪子、反坦克或燃燒彈)。
我有GroundTile
那個GroundTileScript
。我實體化GroundTile
了,但我如何告訴當前GroundTile
實體item
化GroundScript
?真的有可能嗎?
uj5u.com熱心網友回復:
所以,假設你有一個GroundTileScript
物件GroundTile
,你可以這樣做。
GameObject tile = Instantiate(tileprefab);
tile.GetComponent<GroundTileScript>().Spawnitem;
我可能是錯的,所以如果出了什么問題,請告訴我。
uj5u.com熱心網友回復:
您已經在可變溫度下保存了 groundTile 游戲物件資料。
GameObject temp = Instantiate(groundTile, nextSpawnPoint, Quaternion.identy);
您要做的就是從temp
gameObject 中獲取您生成的組件,GameObject temp
如下所示。
GroundTile tile = temp.GetComponent<GroundTile>();
然后您可以使用您的公共方法或獲取公共變數值。
tile.DoSomeThing();
tile.somePublicVariable = 0;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469050.html