Cant Add Component To A Prefab Script

Hello. I am new at c# and unity. The problem that im facing is ;
I created such a script to destroy prefab instances(they are moving towards an axis and when they arrive a Z value I aim to destroy them.)(the value will be taken from a gameobject(children))

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

public class Destroyer : MonoBehaviour
{
    public Transform destroyPosition;
    void Update ()
    {
        if(transform.position.z < destroyPosition.position.z)
        {
            Destroy(gameObject);
        }
    }
}

and attached this to a gameobject prefab.!

When I try to attach destroypoint to the destroyer script destroy position, it doesnt be attached into the prefab. But when i pull the prefab into the screen, on the instance I can attach the destroy position…

Where is the problem? Why cant i put the destroy position object into the destroy position in the prefab? Where do i do the mistake? Thanks for your explanation :slight_smile:

That’s because you’re trying to attach script you the prefab itself. You cannot attach gameobject from particular scene to general prefab, only to gameobject on the same scene. So, if you want to create few instances from prefab, you have to initialize destroyPosition right in the script. Use singleton or FindGameObjectWithTag to initialize it.

It would be good if anyone helped me

I know it has been almost over 5 years, but have you got an answer? I am looking for the exact same problem