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
  4. Dismiss Notice

Falling Platform respawning with multiple clones?

Discussion in 'Scripting' started by ChildishLambino, Oct 8, 2020.

  1. ChildishLambino

    ChildishLambino

    Joined:
    Oct 8, 2020
    Posts:
    2
    Hello! I am trying to create falling platforms that respawn soon after, however, the platform often spawns multiple clones. Please can someone help me with the code so that the falling platform respawns once until it falls and is destroyed again? I have followed a tutorial and used these scripts. I have one script attached to the Platform itself and one attached to an empty Game Object called Platform Manager.

    The script attached to the Platform is:

    public class FallingPlatform : MonoBehaviour
    {
    private Rigidbody2D rb;
    private Transform originalTransform;

    // Use this for initialization
    private void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
    if (col.gameObject.name.Equals("Ellen"))
    {

    Invoke("DropPlatform", 0.5f);
    respawn(gameObject, 2f);
    }
    }

    private void InvokeRepeating(string v1, float v2)
    {
    throw new NotImplementedException();
    }

    private void respawn(GameObject gameObject, float v)
    {
    gameobject.transform.position = originalTransform.position
    rb.isKinematic = true;
    }

    private void DropPlatform()
    {
    originalTransform = gameobject.transform;
    rb.isKinematic = false;
    }

    The script attached to the Platform Manager is:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlatformManager : MonoBehaviour
    {

    public static PlatformManager Instance = null;

    [SerializeField]
    GameObject platformPrefab;

    void Awake()
    {
    if (Instance == null)
    Instance = this;
    else if (Instance != this)
    Destroy(gameObject);

    }

    IEnumerator SpawnPlatform(Vector2 spawnPosition)
    {
    yield return new WaitForSeconds(2f);
    Instantiate(platformPrefab, spawnPosition, platformPrefab.transform.rotation);
    }

    }

    Thank you in advance for any replies.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    Use code tags
     
  3. ChildishLambino

    ChildishLambino

    Joined:
    Oct 8, 2020
    Posts:
    2
    Sorry mate, this is my first time posting on the forums. Here you go.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformManager : MonoBehaviour
    6. {
    7.  
    8.     public static PlatformManager Instance = null;
    9.  
    10.     [SerializeField]
    11.     GameObject platformPrefab;
    12.  
    13.     void Awake()
    14.     {
    15.         if (Instance == null)
    16.             Instance = this;
    17.         else if (Instance != this)
    18.             Destroy(gameObject);
    19.  
    20.     }
    21.  
    22.     IEnumerator SpawnPlatform(Vector2 spawnPosition)
    23.     {
    24.         yield return new WaitForSeconds(2f);
    25.         Instantiate(platformPrefab, spawnPosition, platformPrefab.transform.rotation);
    26.     }
    27.  
    28. }
    29.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FallingPlatform : MonoBehaviour
    6. {
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void OnCollisionEnter2D(Collision2D col)
    17.     {
    18.         if (col.gameObject.name.Equals("Player-Johnny"))
    19.         {
    20.             PlatformManager.Instance.StartCoroutine("SpawnPlatform",
    21.                     new Vector2(transform.position.x, transform.position.y));
    22.             Invoke("DropPlatform", 0.5f);
    23.             Destroy(gameObject, 2f);
    24.         }
    25.     }
    26.  
    27.     void DropPlatform()
    28.     {
    29.         rb.isKinematic = false;
    30.     }
    31. }
    32.