Help with Prefabs

Hi,

I’m creating a driving game in Unity2D. When the player’s car flips over I have a script to respawn their car.
However, I’m unsure as to how to delete the current player and then keep track of the prefab that has been instantiated.
Ex: I have a camera script set to follow the player but when the player dies and a prefab is instantiated I don’t know how to set the clone as the camera target.

Here’s my Respawn script.

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

public class Respawn_and_Stuff : MonoBehaviour
{
    public Vector3 position;
    public Transform self;
    public GameObject myPrefab;

    void FixedUpdate()
    {
        
        position = self.position;
              
    }
        void OnTriggerEnter2D(Collider2D colInfo)
    {
        if (colInfo.CompareTag("World"))
        {
           Instantiate(myPrefab, position, Quaternion.identity);
           
        }
    }
}

And here’s the camera script.

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

public class Follow_Wagon : MonoBehaviour
{

    public Transform target;

    void FixedUpdate()
    {
        Vector3 newPosition = target.position;
        newPosition.z = -10;

        transform.position = newPosition;
    }

}

Any help would be greatly appreciated! Thank you!

Add a Function to reset “target” in your Camera Script,

call the function after you instantiate the new prefab

1 Like