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

Question I need help

Discussion in 'Getting Started' started by unity_guUMuGaFKfAAfw, Jul 5, 2023.

  1. unity_guUMuGaFKfAAfw

    unity_guUMuGaFKfAAfw

    Joined:
    Aug 6, 2021
    Posts:
    1
    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
    here are the codes:
    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;
    }
    }
    }
    }
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    How many times do you newbies want telling about using CODE tags?!