Cant Add Component To A Prefab Script
Asked Answered
R

3

0

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:

Roberts answered 6/4 at 15:10 Comment(0)
R
0

It would be good if anyone helped me

Roberts answered 1/6, 2019 at 12:52 Comment(0)
N
0

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.

Nighthawk answered 6/6, 2023 at 2:34 Comment(0)
C
0

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

Cornetcy answered 6/4 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.