I have code that generate order of obstacles:
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;
}
}
In the void Start()
there is line:
item = roadList[i];
This line is an obstacle that I want to spawn (box, barricade, wheels, antitank or molotov).
I have GroundTile
that has GroundTileScript
. I instantiate GroundTile
, but how do I tell to the current GroundTile
to instantiate item
from GroundScript
? Is it actually possible?
CodePudding user response:
So, assuming you have a GroundTileScript
in the GroundTile
object, you can do this.
GameObject tile = Instantiate(tileprefab);
tile.GetComponent<GroundTileScript>().Spawnitem;
I might be wrong, so tell me if something goes wrong.
CodePudding user response:
You already saving groundTile gameObject data at variable temp.
GameObject temp = Instantiate(groundTile, nextSpawnPoint, Quaternion.identy);
All you gonna do is get component from temp
gameObject what you spawned GameObject temp
like below.
GroundTile tile = temp.GetComponent<GroundTile>();
And then you can use your public methods or get public variable value.
tile.DoSomeThing();
tile.somePublicVariable = 0;