Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by koutrakisa · Nov 27, 2020 at 12:23 PM · coroutinefunctionstartfunction callorder-of-execution

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Nov 27, 2020 at 12:46 PM 0
Share

perhaps adding some code samples of what you are currently doing might help in solving this problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How i can make a function call and then after 15 sec another function call during this function call the first one stops.And this process keep repeating again and again. 1 Answer

Unity hangs on start when finding gameobject 0 Answers

Execution order when Start is declared with IEnumerator return (as a Coroutine) 1 Answer

Acces variables before start() 1 Answer

Coroutine a function within a loop? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges