Reputation:
I have 2 objects:
In the GameManager script I'm instantiating a stripe every 2 seconds, and in the Stripe script I'm instantiating Tiles using for loop with a random number and I'm parenting them to the stripe just created.
My problem is that I want to find out the number the of tiles per instantiated Stripe?
I mean using newStripe.transform.childCount
won't work because it will always return zero, and just for testing reasons I edited the Stripe prefab by adding an empty gameobject and then applied the changes newStripe.transform.childCount
will return 1.
I know that I'm supposed to use object pooling techniques in such situations, but I'm a beginner and I'm trying to learn.
// GameManager Script
void Update()
{
timeBetweenSpawns -= Time.deltaTime;
if (timeBetweenSpawns < -2)
{
Stripe newStripe = Instantiate(stripePrefab, spawnPosition,
Quaternion.identity);
// This variable (tilesCountIntStripe) always returns zero
tilesCountInStripe = newStripe.transform.childCount;
timeBetweenSpawns = 0;
}
}
// Stripe Script
void Update()
{
if (createTile)
{
int randomStripesCount = Random.Range(1, 10);
for (int i = 0; i < randomStripesCount; i++)
{
Tile newTile = Instantiate(tile);
newTile.transform.SetParent(transform, true);
newTile.transform.localPosition = new Vector2(transform.position.x, (-i * (1.1f)));
tilesCount += 1;
}
createTile = false;
}
}
Upvotes: 1
Views: 576
Reputation: 15951
0
where you're asking because Tile::Update()
hasn't run yetIf you want that code to run immediately, I suggest doing something like this:
// GameManager Script
void Update()
{
timeBetweenSpawns -= Time.deltaTime;
if (timeBetweenSpawns < -2)
{
Stripe newStripe = Instantiate(stripePrefab, spawnPosition,
Quaternion.identity);
tilesCountInStripe = newStripe.GetComponent<Stripe>().Init();
//Or whatever the exact name of your class is ^ here
timeBetweenSpawns = 0;
}
}
// Stripe Script
public int Init()
{
int randomStripesCount = Random.Range(1, 10);
for (int i = 0; i < randomStripesCount; i++)
{
Tile newTile = Instantiate(tile);
newTile.transform.SetParent(transform, true);
newTile.transform.localPosition = new Vector2(transform.position.x, (-i * (1.1f)));
tilesCount += 1;
}
return randomStripesCount;
}
Changes:
Init
function that is called from code that you control.
createTile
field that prevents the update code (which runs once a frame) from running every frame.
Upvotes: 0