- Home /
how to spawn object in specific location
Hi, I'm beginner in this field. I currently doing 2d platform game where went the player jump on the platform after a few second the platform will drop and be destroy and my GameManager will spawn new platform but there two problem that I need to solve
How to specifically spawn the new platform in specific location
How to specifically know which platform that need to spawn (currently I have 3 platform in the
This is my platform Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
Rigidbody2D rb2d;
public bool goingFall;
public bool isDown;
public bool isDestroy;
// Start is called before the first frame update
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
StartCoroutine(PlatformFall());
}
if (other.gameObject.CompareTag("Destroy"))
{
isDestroy = true;
StartCoroutine(PlatformDestroy());
}
}
IEnumerator PlatformFall()
{
yield return new WaitForSeconds(2f);
rb2d.bodyType = RigidbodyType2D.Dynamic;
isDown = true;
}
IEnumerator PlatformDestroy()
{
yield return new WaitForSeconds(2f);
Destroy(this.gameObject);
}
and this is my spawn script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformSpawn : MonoBehaviour
{
public GameObject platform;
public Vector3 platformLoc;
Platform platformScript;
// Start is called before the first frame update
void Start()
{
platformScript = GameObject.Find("Platform").GetComponent<Platform>();
}
// Update is called once per frame
void Update()
{
if (platformScript.isDestroy)
{
StartCoroutine(SpawnThePlatform());
}
}
IEnumerator SpawnThePlatform()
{
yield return new WaitForSeconds(1f);
Instantiate(platform, platformLoc, Quaternion.identity);
}
}
Answer by rage_co · Sep 25, 2021 at 04:09 PM
Instead of destroying the object and instantiating another in it's place, which is both inconvinient and heavy at the same time...just disable and later enable the gameobject...using GameObject.SetActive() .....and on a side note, id recommend using animations and particles to clearly convey when the platforms are going to fall.....hope this helps
Well that was a brilliant idea I think I going to test this out first
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Rotation problem with spawning bullet 2 Answers
Errors with Random Instantiate Script(Solved) 2 Answers
Editor Script to place prefabs with some variance 0 Answers
Problem with Random.range 1 Answer