Hello, I am making a game that u have a gun that shoots teleporters but i need help.
When i create 2 prefab teleporters by Instantiate
but when I am creating them they are not connected (teleporter1 and not teleporter1(Clone))
pls help me
Bullet.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 20f;
public Rigidbody2D rb;
public GameObject teleport1;
public GameObject teleport2;
private static GameObject spawnedTeleport1;
private static GameObject spawnedTeleport2;
void Start()
{
rb.velocity = transform.right * speed;
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
Vector2 hitPosition = transform.position;
Debug.Log("Hit: " + hitInfo.name + " at position: " + hitPosition);
if (teleport1 != null && spawnedTeleport1 == null)
{
spawnedTeleport1 = Instantiate(teleport1, hitPosition, Quaternion.identity);
}
else if (teleport2 != null && spawnedTeleport2 == null)
{
spawnedTeleport2 = Instantiate(teleport2, hitPosition, Quaternion.identity);
}
Destroy(gameObject);
}
}
Teleporter.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleporter : MonoBehaviour
{
[SerializeField] private Transform destination;
GameObject player;
GameObject box;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
box = GameObject.FindGameObjectWithTag("Box");
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
if (Vector2.Distance(player.transform.position, transform.position) > 0.3f)
{
player.transform.position = destination.transform.position;
}
}
if (collision.CompareTag("Box"))
{
if (Vector2.Distance(box.transform.position, transform.position) > 0.3f)
{
box.transform.position = destination.transform.position;
}
}
}
}