Access a GameObject script from a Prefab script

Hi!
I am doing a little game, where the player controlls a prefab object, and when it collides, the player loses control over it, and a new prefab is created.
To generate this new prefab, i have created a GameObject that has a generator script (2nd script), but to know if it has to generate the prefab, i need the Prefab-Player script to feed into it and tell it to generate a new one, if it has collided (1st script).

Here comes the problem though, you can’t take reference of a GameObject in a Prefab that has not yet been Instantiated.

Is there any way I could do something like this or is it impossible?

Thanks for any help!! :smiling_face:


Player script:

void OnCollisionEnter(Collision collision)
{
    if (collision.collider.gameObject.layer == 6)
    {
        crashed = true;
        rocketGenerator.GetComponent<RocketGen>().destroyed = true;
    }
    else if (collision.collider.gameObject.layer == 3 && !completed)
    {
        completed = true;
        var endTime = Time.time - startTime;
        Debug.Log("Completed in " + endTime.ToString("F2") + " seconds!");
    }
}

Prefab-Gen script:

using UnityEngine;

public class RocketGen : MonoBehaviour
{
    [SerializeField] private GameObject rocket;

    public bool destroyed;
    public int rocketsGenerated;
 
    // Start is called before the first frame update
    void Start()
    {
        PlaceRocket();
    }

    // Update is called once per frame
    void Update()
    {
        if (destroyed)
        {
            destroyed = false;
            PlaceRocket();
            rocketsGenerated += 1;
        }
    }

    void PlaceRocket()
    {
        Instantiate(rocket, new Vector3(0f, 4.2f, 0f), Quaternion.identity);
    }
}

Well, you can give the GameObject with the Prefab-Gen script a unique name and then in the script attached to the prefab do:

void Start()
{
     referenceGameObject = GameObject.Find("NAME");
}

and then use this GameObject as the reference.