how to teleport to a prefab and then destroying it

Hi,

I want to make a basic game mechanic which is player teleports to a position of a dagger he throws. My problem is when i instantiate the dagger it doesn’t move and when i teleport it teleports to the initial position of the dagger.

My code as it follows;

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

public class UseDagger : MonoBehaviour
{
    public GameObject daggerPrefabs;
    public bool isDaggerEquipped;
    Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        isDaggerEquipped = true;
        offset = new Vector3(0.4f, 0, 0.2f);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space) && isDaggerEquipped)
        {
            Instantiate(daggerPrefabs, transform.position + offset, Quaternion.identity);   
            daggerPrefabs.transform.Translate(Vector3.forward * Time.deltaTime, Space.Self);
            isDaggerEquipped = false;

        }

        if(Input.GetKeyDown(KeyCode.Space) && !isDaggerEquipped)
        {
            transform.position = daggerPrefabs.transform.position;
            isDaggerEquipped = true;
            
        }
    }
}

And also i want to destroy or remove the dagger player threw so there can’t be more than one dagger at the scene but Unity doesn’t allow me to destroy prefabs to avoid data loss by destroying asset as it says.

Could you please help me about that?

Hello! I think I understand what you want, here’s some untested code, but it should work (for 2D at least)

//This is similar to public but you can't change it from other scripts (feel free to change it)
        [SerializeField] private float Force;

        //Have to set a reference to the spawned Dagger
        GameObject SpawnedDagger = 
        Instantiate(daggerPrefabs, transform.position + offset, Quaternion.identity);


        //Gets direction (from mouse) to know where to launch it to
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 direction = (Vector2)mousePos - (Vector2)transform.position;

        //Adds force to Rigidbody, is affected by mass and drag
        SpawnedDagger.GetComponent<Rigidbody2D>().AddForce(direction * Force, ForceMode2D.Impulse);

And to teleport to it, you can

transform.position = SpawnedDagger.transform.position;

@rez186

Thanks for the answer chanwolf.