Search Unity

Making an infinite runner, script wont spawn platforms

Discussion in 'Scripting' started by rublenathaniel, Feb 12, 2018.

  1. rublenathaniel

    rublenathaniel

    Joined:
    Feb 12, 2018
    Posts:
    1
    So a little while back i started working on a joke/side project. It's a simple infinite runner that i am mainly using to help figure out coding in c#. When you start playing the game the system automatically spawn ten platforms, and when a platform falls it is supposed to spawn another platform and then get deleted. My problem is that after the first ten platforms are spawned they will fall and get deleted without spawning new platforms. Below are all of my scripts linked to the platforms and how they work.

    spawningStartingPlatforms:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnStartingPlatforms : MonoBehaviour {
    6.  
    7.     static public int amount;
    8.     public GameObject platform;
    9.     public float horizontalMin = 14f;
    10.     public float horizontalMax = 19f;
    11.     public float verticalMin = -5f;
    12.     public float verticalMax = 5f;
    13.     private Vector2 originPosition;
    14.  
    15.     void Start () {
    16.         originPosition = transform.position;
    17.         Spawn ();
    18.     }
    19.  
    20.     void Spawn() {
    21.         for (int i = 0; i < 10; i++) {
    22.             Vector2 randomPositon = originPosition + new Vector2 (Random.Range (horizontalMin, horizontalMax), Random.Range (verticalMin, verticalMax));
    23.             Instantiate (platform, randomPositon, Quaternion.identity);
    24.             originPosition = randomPositon;
    25.         }
    26.     }
    27. }
    28.  
    spawnPlatforms:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnPlatforms : MonoBehaviour {
    6.  
    7.     static public bool Spawn = false;
    8.     public bool canSpawn = true;
    9.     public GameObject platform;
    10.     public float horizontalMin = 14f;
    11.     public float horizontalMax = 19f;
    12.     public float verticalMin = -5f;
    13.     public float verticalMax = 5f;
    14.     private Vector2 originPosition;
    15.  
    16.     void Start () {
    17.         originPosition = transform.position;
    18.     }
    19.  
    20.     void update() {
    21.         if (canSpawn == true && Spawn == true){
    22.             Vector2 randomPositon = originPosition + new Vector2 (Random.Range (horizontalMin, horizontalMax), Random.Range (verticalMin, verticalMax));
    23.             Instantiate (platform, randomPositon, Quaternion.identity);
    24.             originPosition = randomPositon;
    25.             canSpawn = false;
    26.             Spawn = false;
    27.         }
    28.     }
    29. }
    platformFall:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class platformFall : MonoBehaviour {
    6.  
    7.     public bool falling;
    8.     public float fallTime;
    9.     static public bool Spawn = false;
    10.     public float FallDelay = 3;
    11.  
    12.     void OnCollisionEnter2D (Collision2D other){
    13.         if (other.gameObject.CompareTag ("Player"))
    14.             Invoke ("Fall", FallDelay);
    15.     }
    16.  
    17.     void Fall(){
    18.         GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    19.         fallTime = Time.deltaTime;
    20.     }
    21.     void OnTriggerEnter2D (Collider2D other){
    22.         if (other.gameObject.CompareTag ("deleteEvery")) {
    23.             Spawn = true;
    24.             Destroy (gameObject);
    25.         }
    26.     }
    27. }
    28.  
    I would be very thankful for any help you people can provide.
     
  2. rythemmeon

    rythemmeon

    Joined:
    Jan 17, 2018
    Posts:
    4
    Are there any errors being generated or is the function not being called at all?