Search Unity

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

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

  1. noneforyou

    noneforyou

    Joined:
    Jan 7, 2021
    Posts:
    4
    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;
    4.  
    5. public class Gameplay : MonoBehaviour
    6. {
    7.     // original prefab
    8.     public GameObject originalRook;  // yes it is a chess themed game
    9.  
    10.     void Start()
    11.     {
    12.         spawnRook(3, 1);
    13.     }
    14.  
    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. }
    22.  
     
    Last edited: Sep 25, 2022
  2. koirat

    koirat

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

    MelvMay

    Unity Technologies

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

    I'll move your post.

    Thanks.
     
    Bunny83 likes this.
  4. noneforyou

    noneforyou

    Joined:
    Jan 7, 2021
    Posts:
    4
    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

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,929
    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

    noneforyou

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