Search Unity

Help with create random objects

Discussion in 'Scripting' started by Shoonky, May 25, 2017.

  1. Shoonky

    Shoonky

    Joined:
    May 24, 2017
    Posts:
    10
    Hello, I tried to make it in notifications, the generation of objects will be in the moment the character jumps for the first time, but I just want it to be called once, and not to be done continuously, the problem is that every time That jump is called to the generation of objects and they are created too many by the screen when it should only be called once, how can I limit the call?

    Code (CSharp):
    1. public class generadorXY : MonoBehaviour {
    2.     public Transform[] spawn;
    3.     public float Tmin = 1f;
    4.     public float Tmax = 1f;
    5.     public Animator animator;
    6.     private Animation plataformas;
    7.  
    8.     public GameObject Plataformas;
    9.  
    10.     // Use this for initialization
    11.     void Awake(){
    12.         animator = GetComponent<Animator> ();
    13.     }
    14.  
    15.     void Start () {
    16.         NotificationCenter.DefaultCenter ().AddObserver (this, "PersonajeSalta");
    17.  
    18.     }
    19.     void PersonajeSalta (){
    20.         plataformas = FindObjectOfType<Animation>();
    21.         this.InvokeRepeating ("spawnXY", Tmin, Tmax);
    22.  
    23.     }
    24.     // Update is called once per frame
    25.     void Update () {
    26.        
    27.     }
    28.     void spawnXY(){
    29.         int spawnIndex = Random.Range (0, spawn.Length);
    30.         Instantiate (Plataformas, spawn [spawnIndex].position, spawn[spawnIndex].rotation);
    31.  
    32.     }
    33. }
    Thanks for help!!
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. public class generadorXY : MonoBehaviour {
    2.     public Transform[] spawn;
    3.     public float Tmin = 1f;
    4.     public float Tmax = 1f;
    5.     public Animator animator;
    6.     private Animation plataformas;
    7.     private bool jumped = false;
    8.  
    9.     public GameObject Plataformas;
    10.  
    11.     // Use this for initialization
    12.     void Awake(){
    13.         animator = GetComponent<Animator> ();
    14.     }
    15.  
    16.     void Start () {
    17.         NotificationCenter.DefaultCenter ().AddObserver (this, "PersonajeSalta");
    18.  
    19.     }
    20.     void PersonajeSalta (){
    21.         plataformas = FindObjectOfType<Animation>();
    22.         this.InvokeRepeating ("spawnXY", Tmin, Tmax);
    23.  
    24.     }
    25.     // Update is called once per frame
    26.     void Update () {
    27.        
    28.     }
    29.     void spawnXY(){
    30.         if(!jumped)
    31.         {
    32.         jumped = true;
    33.         int spawnIndex = Random.Range (0, spawn.Length);
    34.         Instantiate (Plataformas, spawn [spawnIndex].position, spawn[spawnIndex].rotation);
    35.         }
    36.     }
    37. }
     
  3. Shoonky

    Shoonky

    Joined:
    May 24, 2017
    Posts:
    10
    thaks for help, but with your
    Thanks for the help, but in its solution something else happens, now it only runs once, that was expected, but only generates an object and does not generate them continuously, the initial error was that every time the character jumped, Lots of objects, thank you very much
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    My code was a guess at witch peace did the jumping. So use my example to figure out where the if statement should go.