Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Resolved The instantiated "clones" of my prefab don't work like the original.

Discussion in 'Scripting' started by noneforyou, Sep 25, 2022.

  1. noneforyou


    Jan 7, 2021
    Hi, I'm new to Unity, and I couldn't find anything pinpointing my issue anywhere.

    I have an enemy I need to duplicate, so I made a prefab for it, but the prefab doesn't have the completed fields the original has for its targeting component. I need the enemy to target the player, but for some reason the enemy's script that had a public variable for the player's transform was empty. Thus, I made my player a prefab, so I could drag it into the empty variable. Now nothing works. Why? It doesn't make sense because the prefab's instantiated clones should have the correct player to chase, yet they just sit there and do nothing.

    I have the original GameObject on the hierarchy, and the clone of the prefab of it, they are identical, yet the original GameObject targets the player, whilst the clone doesn't.

    If I don't make anything a prefab, everything works flawlessly. Am I missing a key detail?

    I don't know what could help, but here's some stuff:

    1. Code for instantiating prefab

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class Gameplay : MonoBehaviour
    6. {
    7.     // original prefab
    8.     public GameObject originalRook;  // yes it is a chess themed game
    10.     void Start()
    11.     {
    12.         spawnRook(3, 1);
    13.     }
    15.     // params = x coord and y coord to spawn rook
    16.     void spawnRook(float x, float y) {
    17.         Vector3 spawnLocation = new Vector3(originalRook.transform.position.x + x, originalRook.transform.position.y + y, 0);
    18.         GameObject rook = Instantiate(originalRook, originalRook.transform);
    19.         rook.transform.position = spawnLocation;
    20.     }
    21. }
    Last edited: Sep 25, 2022
  2. koirat


    Jul 7, 2012
    You have to set the target after instantiating, since scene references are not saved into prefab.
    Bunny83 likes this.
  3. MelvMay


    Unity Technologies

    May 24, 2013
    Please post general scripting questions in the Scripting forum and not the 2D forum. This forum is for 2D specific

    I'll move your post.

    Bunny83 likes this.
  4. noneforyou


    Jan 7, 2021
    How would I do that? I get an error, "instantiate objects with a parent which is persistent. New object will be created without a parent." which I don't understand, since the object I'm trying to instantiate is the parent. Isn't it impossible to make a child a prefab?
  5. spiney199


    Feb 11, 2021
    Right now your rooks have a reference to the player prefab, and this prefab is not the same object that you have in your scene, that is an instance of the prefab.

    You want to get that instance of player and set that as your instantiated rook's target.

    So in simple steps:
    • Have a reference to the player instance (this can be done through serialised/public fields in the inspector)
    • Instantiate the Rook
    • Set the Rook's target as the referenced instance of the player
    This should be a good lesson in the difference between scene level objects, and asset level objects.
    Last edited: Sep 26, 2022
    Bunny83 and noneforyou like this.
  6. noneforyou


    Jan 7, 2021
    Tysm man :) I fixed it. It took a while.