Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to instantiate two objects at the same time (SOLVE)

Discussion in 'Scripting' started by Venatal, Jan 13, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    How can I modify this code so that the two objects are instantiated at the exact time.
    Code (CSharp):
    1.  
    2. public void DoubleSpawn()
    3.     {
    4.         DS = true;
    5.         int On = Random.Range(2, 3);
    6.         int No = Random.Range(0,1);
    7.         Instantiate(SquarePrefab[No], transform.position, rotations[No]);
    8.         Instantiate(SquarePrefab[On], transform.position, rotations[On]);
    9.         SquareMovement.Instance.IncreaseSpeed(speed);
    10.     }
    11.  
    In game when the two objects spawn they seem to spawn at different times or travel at different speeds either way they reach the collision point (which is of equal distance) at different times. so I was wondering how I can change the script so that they spawn at the same time.
    The clones spawn in the same position but should not interact with each other.
    Thanks for all the help.
     
  2. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    You can't really control the instantiation time of an object in Unity - it's not something that your design should be dependent on. That said, the way Unity works means that if you have two objects that are created with the exact same profiles, and one is reaching a target a great deal before the other, there must be something else happening along the way. Check what's happening during each call to Update().

    More than that, there isn't enough code above to diagnose what's happening.
     
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Its not a big difference between the collision times however it is very noticeable about 0.1-0.2 ms difference.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not correct...all functions, unless specifically documented as async, run "at the same time". That is, during one frame. The code technically takes some CPU cycles to run, but essentially as soon as the function has been called, that code is finished and the object is fully instantiated. Anyway, if you call Instantiate twice like that, the objects are instantiated during the same frame, and for all intents and purposes they are instantiated at the same time.

    --Eric
     
    image28, Fajlworks and Kiwasi like this.
  5. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    hmm, I just noticed that when using deep profile.
    What do you think could be causing it to collide at different times then?
    **Turns out it was the speed but its not making any sense to me, the speed of each clone should be 0.15 faster than the last. one seems to be 2.00 and the other 2.90.**
     
  6. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    Well, yes, if you look at it from that distance, this is true. I should have been more specific - the methods don't run at exactly the same time on the CPU. That said, even if they are all triggered from the start of one frame - methods being instantiated in a single frame in Unity aren't really called 'at the same time', does it? As far as I'm aware, that's only possible if you're using multiple cores and multiple threads, which I don't believe is supported at that part of Unity's arch...

    What I was implying was that there isn't any way to literally make two things instantiate at the same time. There's only that small scope of 'in a single frame'. So if an implementation creates two objects at the same time, then those objects should be effected equally enough by the engine - which means if the two objects the OP mentions truly are the same in every respect, then if they reach their target at noticeably different times (i.e., on different frames) then there's got to be something happening along the way.

    It's hard to say without more code to go on.
     
  7. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Okay, I seemed to have fixed it.
    This is the code now:
    Code (CSharp):
    1.  
    2. [LIST=1]
    3. [*]public void DoubleSpawn()
    4.     {
    5.         DS = true;
    6.         int On = Random.Range(2, 3);
    7.         int No = Random.Range(0, 1);
    8.         Instantiate(SquarePrefab[No], transform.position, rotations[No]);
    9.         SquareMovement.Instance.IncreaseSpeed(speed);
    10.         Instantiate(SquarePrefab[On], transform.position, rotations[On]);
    11.         SquareMovement.Instance.IncreaseSpeed(speed);
    12.     }
    13. [*]
    14. [/LIST]
    15.  
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I was responding to the line about "You can't really control the instantiation time of an object in Unity - it's not something that your design should be dependent on," which isn't correct, since your design absolutely can depend on it. The point is that Instantiate is 100% deterministic, since it returns immediately, and using it twice in the same frame counts as "at the same time" even if that's not quite literally true.

    --Eric
     
    Fajlworks and Kiwasi like this.
  9. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Ohh, so are all scripts run in a component run in a single frame?
    Like this for example:
    Code (CSharp):
    1.  
    2. void OnCollisionEnter2D(Collision2D col)
    3.     {
    4.         if (col.gameObject.CompareTag(gameObject.tag))
    5.         {
    6.                 //Do stuff
    7.         }
    8.         else
    9.         {
    10.             //Do stuff
    11.         }
    12.         Destroy(gameObject);
    13.     }
    14.  
    You'd think i'd know that by now. :p
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  11. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Thanks for the link! :)