Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Objects instantiating at 0,0,0 occasionally (bug?)

Discussion in 'Scripting' started by Shibilicious, Aug 11, 2015.

  1. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    On this scene, I only have 2 game objects. 1 empty object with a spawner script, and 1 with sprite renderers, colliders, and etc for the map.

    The spawner script instantiates enemy ships on each spawn points at the start function.

    Map:


    My spawn points are those white circles outside the star.

    What should happen:


    As you can see, the enemy ships are on their corresponding position.

    The enemy ships contains a sprite renderer, polygon collider, and a rigidbody.

    What happens often:


    At the bottom left, you see some enemy ships spawning at 0,0 for some reason I can't figure out....

    Spawn Script (simplified) (yeah, bug still occurs with this script):
    Code (csharp):
    1.     public SpawnPoints top;
    2.     public GameObject t1Enemy;
    3.     public float spawnRate;
    4.  
    5.     void Start () {
    6.         StartCoroutine(Spawn());
    7.     }
    8.  
    9.     IEnumerator Spawn () {
    10.         while (true) {
    11.             T1Spawn();
    12.  
    13.             yield return new WaitForSeconds(spawnRate);
    14.         }
    15.     }
    16.  
    17.      void T1Spawn () {
    18.             Instantiate(t1Enemy, top.one.position, top.one.rotation);
    19.             Instantiate(t1Enemy, top.two.position, top.two.rotation);
    20.             Instantiate(t1Enemy, top.three.position, top.three.rotation);
    21.             Instantiate(t1Enemy, top.four.position, top.four.rotation);
    22.             Instantiate(t1Enemy, top.five.position, top.five.rotation);
    23.             Instantiate(t1Enemy, top.six.position, top.six.rotation);
    24.             Instantiate(t1Enemy, top.seven.position, top.seven.rotation);
    25.             Instantiate(t1Enemy, top.eight.position, top.eight.rotation);
    26.             Instantiate(t1Enemy, top.nine.position, top.nine.rotation);
    27.       }
    TL;DR: Game Objects are spawning at 0,0 occasionally even if I stated on the script to spawn them at the specific spawn points.

    What am I doing wrong?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Are you changing or assigning the SpawnPoints from some other script?
     
  3. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Nope. It's at the same script.
    Code (csharp):
    1.  
    2. public class SpawnPoints {
    3.     public Transform one, two, three, four, five, six, seven, eight, nine;
    4. }
    It's where I put the spawn point's transform.
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Though this doesn't help with the problem, but maybe you should consider using an array instead of nine separate variables.
     
  5. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Eh, I wouldn't see the difference on this one and I'll end up doing top.spawn[0] which looks terrible in my eyes.

    On topic:
    Forgot to mention that when this bug occurs, it drops my frame rate to 0 in little time to the point it sort of crashes Unity.
     
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    The benefit would be quite obvious, if you want to add more spawn points you can do so easily. It also makes the code much more readable.

    Code (csharp):
    1.  
    2. public class SpawnPoints
    3. {
    4.     public Transform points[];
    5. }
    6.  
    7. public SpawnPoints top;
    8.     public GameObject t1Enemy;
    9.     public float spawnRate;
    10.  
    11.     void Start () {
    12.         StartCoroutine(Spawn());
    13.     }
    14.  
    15.     IEnumerator Spawn () {
    16.         while (true) {
    17.             T1Spawn();
    18.  
    19.             yield return new WaitForSeconds(spawnRate);
    20.         }
    21.     }
    22.  
    23.      void T1Spawn () {
    24.         for(int i = 0; i < top.points.length; i++)
    25.             Instantiate(t1Enemy, top.points[i].position, top.points[i].rotation);
    26.      }
    27.  
     
    Shibilicious likes this.
  7. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Not only that, but it reduces copy & paste errors as well. But like I've said, it's off-topic. I just meant to give a friendly suggestion.
     
    Shibilicious likes this.
  8. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Wow! That's actually what I was trying to do before but gave up on it due to the lack of knowledge. I should've asked what would it benefit me earlier instead of being confident that there's no difference.

    Thanks, Heuvel and blizzy!

    Though I'm still figuring out how to fix this bug...
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    is it always the same ones that spawn in the wrong place? All correctly assigned in inspector?
     
  10. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Nope, it's random.

    Basically, if I load the scene, there will be a chance that this: http://puu.sh/jxqPH/009613a86c.png, will happen.

    There's also a chance that it doesn't happen when I load the scene and this bug won't occur over time.
     
  11. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Is there any behaviour on the enemies that may cause the to move
     
  12. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    On this scene, no. I turned off my enemy ai script and this still occurs.

    Also, on top of this bug, game objects that have rigidbody, when they move towards the wall, they teleport back to 0,0 including my player ship.

    I was surprised because I don't even have a teleporting script yet it teleports my character to 0,0 when I bump into the walls.

    The walls have colliders and a rigidbody that has x, y, z contraints if this information helps.
     
  13. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Boop.

    Still trying to fix the bug.. zz
     
  14. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I've never tried to instantiate more than 2 objects simultaneously, but perhaps you could make the T1 spawn a coroutine as well and use "return yield null;" to skip a frame between instantiations. It'll make each "set" take 9 frames, but if you instantiate them as disabled (or turn their renderer off) and then enable them all when the set is finished, you can make them appear at the same time if that's a big deal to you.

    I just want to see if this might be a problem with too many simultaneously instantiated objects. You should also put little renderers on your spawn points and see if maybe those are what's moving, and not the instantiated objects ignoring them.
     
  15. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Are the spawn points placed in editor or during runtime?
     
  16. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    I tried to create a countdown before I start the spawn coroutine and the bug still occured after 8 times of playing and un playing the scene.

    I also tried spawning only one prefab and the bug still occured after 5 times of playing and un playing the scene.

    The spawn points are placed in the editor, yes.
     
  17. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    I FIXED IT, I HOPE!

    My Enemy Ships Prefab's rigidbody2d has constraints that freezes X and Z. I unchecked them and they don't spawn in the middle anymore. But if I check them back, they spawn in the middle again.

    I also tried turning on the constraints AFTER they spawn and they teleport back to 0,0.

    Rookie mistake... Silly me!

    Thank you all anyway for trying to help me!
     
    ThermalFusion and JoeStrout like this.
  18. The_Darkwing_Duck

    The_Darkwing_Duck

    Joined:
    Jun 16, 2017
    Posts:
    20
    Are you able to turn on the constraints in code? Would they still spawn in the middle?