Search Unity

Instantiating/Spawning

Discussion in 'Scripting' started by luskos, Mar 19, 2018.

  1. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    Hi, guys!
    I try to make a game where you jump from one platform to another, when the player touches the first platform i try to make it so the game spawn the next.Anything i do fails with memory leak and Unity get not responding so i close it trough Task manager, because it instantiate to infinity.Can't figure out how to spawn only one more platform when player get's to the last one.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could try a spawn manager script. Let it have a variable for the prefab to spawn as well as a variable for the 'next valid platform (aka. most recently spawned). It could also have a method that returns a bool, and is passed a game object, to check if the the player is on the most recent one.

    Does that make sense?

    Would you like to understand why unity froze, or you already know?
     
    Homicide likes this.
  3. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Sounds like you're spawning on an infinite loop. Could you post the method that trues to instantiate the object?
     
  4. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    This is what i use:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Spawn : MonoBehaviour {
    7.     private float x = -5.0f;
    8.     private float y = 1.56f;
    9.     private float z = -5.0f;
    10.     public GameObject Platform;
    11.  
    12.     void OnCollisionEnter(Collision col){
    13.         if(col.gameObject.tag == "Platform"){
    14.             z += -10f;
    15.             Instantiate(Platform, new Vector3(x,y,z),Quaternion.identity);
    16.         }
    17.     }
    18. }
    19.  
    Even if i put boolean variable inside the if statement which should act as a flag which allows and disallows instantiation, it does not work, so i removed it.The result is the same.As long as player character, which is a simple cube falls onto first platform everything is ok, it does spawn one more then if i jump on it.It goes infinite spawning.
    It's a script attached on every platform that's made, i've tried attaching it to an empty game object and it won't work at all.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're spawning all the platforms in the same place. You'll want an offset from the current platform position, not an absolute position when you spawn the new platform.
     
  6. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    Not the case, i see them spawning in the distance when Unity freeze.There is this variable z that is changed everytime when there is instantiation.It adds up to the z position of the platform.Thanks for the response.
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I believe @LeftyRighty is correct. Those variables aren't static, so z is only incremented by -10 once. That's assuming this script is attached to the platform since it has the OnCollisionEnter method for your collision.

    So the player hits the platform, a new platform is spawned at -15.0. The player hits the next platform, and that one is also spawned at -15.0 which triggers another OnCollisionEnter() immediately, which spawns another platform at -15.0... and so on forever, which is exactly the behaviour you describe:

     
  8. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    You are partially right, it does spawn more platforms at -15 but also does spawn platforms ahead in distance too.I think my gravity is pushing the player constantly down and initiating new collisions which spawn more platforms.Any suggestions?!
     
  9. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    I don't know the specifics of Unity's collision, but most likely OnCollisionEnter is getting called every frame causing seemingly endless spawning. A bool will work, but if the one you tried didn't, I'm willing to bet you declared it in the function instead of as a member variable. That would cause the bool to fall out of scope and the value lost. If you declare it with the other member variables it should work as long as you check it and set it in the correct spots.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you try my suggestion? :)
     
  11. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    @Pagefile Yeah, when my player is initiating jump, accumulating power while "Jump" button is held the platform he is currently sink with Vector.down same as player, probably that is registering more entry collisions.

    @methos5k I try to put all platforms in array and set bool variable on each platform to check if it's the last created, so when player character get in contact, it spawn the next and set the value to false, so it doesn't spawn more if player jump on it second time.I am still confused with accessing attributes/components.

    EDIT: I've managed to achieve it, had some problems with OnCollisionEnter not working exept when script containing it was attached to the player.Mainly there is lastObject bool on first platform, when player hit it, game reverse it to false, then spawn another platform and make it's own lastObject true.It only spawn new platform if the player collide with platform with this value set to true.Writing code in the morning is the best, because of all the rest!Might attach the script when i check and clear it from leftover code that doesn't work, if there is any.I am a bit of sloppy coder.
     
    Last edited: Mar 20, 2018
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    No, Enter gets called once per discrete collision, Stay gets called every frame, Exit gets called once at the end of a discrete collision.
     
  13. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    My platform sink down when jump is accumulated and the player with it, probably because the way my gravity works it falls behind and re-enter collision in the process.