Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kvnlaw00 · Apr 13, 2020 at 10:05 AM · scripting probleminstantiateunity 2dprefabsdelay

Script inside instantiated prefab run lag behind

Hi everyone, newbie here. I have a problem which script written inside instantiated prefab seem running a bit behind the main script (the script which instantiate the prefab.) I re-write and simplify the script below to simplify the question: The first script: "Counting1" using UnityEngine;

 public class Counting1 : MonoBehaviour
 {
     public Transform counting2Prefab;
     public int count1;
     public static int passCount;
     public float time = 2f;
     public float delay;
 
     void Start()
     {
         count1 = 0;
         delay = time;
     }
 
     void Update()
     {
         if (delay <= 0)
         {
             GetCounting();
             delay = time;
         }
         delay -= Time.deltaTime;
     }
 
     void GetCounting()
     {
         SpawnCounting2();
 
         Debug.Log("count1 = " + count1);
         passCount = count1;
         GetSum();
         Debug.Log("count3 = " + count1);
     }
     void SpawnCounting2()
     {
         Instantiate(counting2Prefab, transform.position, transform.rotation);
     }
     void GetSum()
     {
         count1++;
     }
 }

The second script= "Counting2" using UnityEngine;

 public class Counting2 : MonoBehaviour
 {
 
     private static int count2;
 
     void Start()
     {
         count2 = Counting1.passCount;
         Debug.Log("count2 = " + count2);
         Destroy(gameObject);
     }
 }
 

I need the counting order to be: count1, and then count2, and then count3. But if you run the script above, the order always become: count1, and then count3, and then count2.

The count2 which written inside second script seems always running lag behind. Any way to do it so count2 will be executed before count3? Should i use something like coRoutine?

Thanks for the time!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Casiell · Apr 13, 2020 at 12:31 PM

Instead of Start in Counting2 class use Awake. Awake will be called at the exact moment when you are instantiating a prefab, before execution goes any further. Start is called somewhere later, but I'm not exactly sure when.

Comment
Add comment · Show 1 · 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 kvnlaw00 · Apr 13, 2020 at 01:53 PM 0
Share

i tried to put all the coding inside Awake in Counting2 class. The order somehow changed, become: count2, count1, count3. but the calculation still lagging. Actual first calculation: count2 = 0 count1 = 0 count3 = 1 Second calculation: count2 = 0 count1 = 1 count3 = 2 Note that the count2 in the second calculation is sitll at 0. it supposed to be 1.

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

249 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 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

Make a game object in a scene into a prefab with script 1 Answer

Instatiate to the next scene prefabs from an array 1 Answer

Instantiate Reference Problem 1 Answer

Fireball instantiate - lags 1 Answer

How to stop enemy AI from vibrating when tracking y component 0 Answers


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