- Home /
Call function after other objects have run their Start() function
I instantiate about 100-200 game objects (random number) on an update method and each one of these game objects runs a script that randomizes their stats on Start(). Now I want to get the changed data (in order to pool them)
How can I call another method after all the Start() have played out and get the randomized results. I ve tried using a co-routine and wait for a frame but it seems I don't understand something in function order of execution.
perhaps adding some code samples of what you are currently doing might help in solving this problem.
Answer by koutrakisa · Nov 27, 2020 at 01:42 PM
Level Generator
public void Update()
{
for(int i=0;i<30;i++)
{
Instantiate(RoomPrefab);
}
Room Script
public void Start()
{
for(int i=0;i<10;i++)
{
GameObject temp=Instantiate(MiscGameobjects);
MiscObjectPool .MiscObject_Positions.Add(temp);
}
}
SizeRandomizer // Similarly there is one for Sprite and one for Color
public void Start()
{
transform.scale=new Vector3(Random.Ranger(1.0f,2.0f),Random.Ranger(1.0f,2.0f),Random.Ranger(1.0f,2.0f));
}
MIscObjectPool
public class MiscObjectPool : MonoBehaviour {
public class MiscObject_data
{
public Vector3 position;
public Vector3 scale;
public Color32 color32;
public Sprite sprite;
public MiscObject_data(Vector3 _pos ,Vector3 _scale ,Color32 _color32, Sprite _sprite)
{
this.position = _pos;
this.scale = _scale;
this.color32 = _color32;
this.sprite = _sprite;
}
}
public Dictionary<Vector3, MiscObject_data> MiscObject_Positions
= new Dictionary<Vector3, MiscObject_data>();
public static List<GameObject> MiscGOs = new List<GameObject>();
**// When should i call this function in order to have the randomized MiscObj;**
public void GetRandomized_MiscData()
{
foreach(GameObject miscGO in MiscGOs)
{
MiscObject_data data = new MiscObject_data(miscGO.transform.position,
miscGO.transform.localScale,
miscGO.GetComponent<SpriteRenderer>().color,
miscGO.GetComponent<SpriteRenderer>().sprite);
MiscObject_Positions.Add(data.position, data);
}
}
public void Update()
{
foreach(Vector3 _pos in MiscObject_Positions.Keys)
{
if (Vector3.Distance(_pos, transform.position)<10)
{
// enable a pooled object in spesific _pos
// set its randomized size
// set its randomized Color;
}
}
}
}
I Instantiate MiscObjects like rocks on scene loading , after each rock is instantiated he changes between random rock sprites , color and size .
On camera the player can see around 25-30 rocks simultaneously .So i destroy all the generated rocks but keep the data after the randomizers and save them on a dictionary with their position as the key. So that I can pool 30 rocks instead of almost 200.
Thank you for your time
Answer by GREYSETH · Nov 27, 2020 at 01:31 PM
im not exactly sure if this will work, but maybe make a boolean and name it something like hasCalled
and in the update function just do
void Update()
{
if (!hasCalled)
{
hasCalled = true;
TheFunctionYouWannaCall();
}
}
so that way it will only be called once
if this is not what youre trying to do or if theres something wrong then let me know :)