Search Unity

Cannot instantiate objects with a parent which is persistent.

Discussion in 'Scripting' started by Rayeloy, Jan 27, 2019.

  1. Rayeloy

    Rayeloy

    Joined:
    Feb 12, 2017
    Posts:
    45
    So I'm trying to instantiate some objects and put them as childs of some other transforms. The parents are empty objects on the hierarchy that have no parent(the scene is the parent). The Game Objects I'm trying to instantiate are prefabs that I have stored in the Resources folder.

    Code (CSharp):
    1.  public void CreatePlayer(int playerNumber=0)
    2.     {
    3.         if (!offline)
    4.         {
    5.             playerNum++;
    6.         }
    7.         PlayerMovement newPlayer = Instantiate(playerPrefab,Vector3.zero,Quaternion.identity,playersParent).GetComponent<PlayerMovement>();
    8.         GameObject newPlayerCanvas = Instantiate(playerCanvasPrefab, playersCanvasParent);
    9.         CameraController newPlayerCamera = Instantiate(playerCameraPrefab, playersCamerasParent).GetComponent<CameraController>();
    10.         Camera newPlayerUICamera = Instantiate(playerUICameraPrefab, playersUICamerasParent).GetComponent<Camera>();
    11.  
    12.         newPlayer.gameObject.name = "Player";
    13.         if (!offline)
    14.         {
    15.             newPlayer.gameObject.name = newPlayer.gameObject.name + playerNum;
    16.         }
    17.         else
    18.         {
    19.             newPlayer.gameObject.name = newPlayer.gameObject.name + playerNumber;
    20.         }
    21.  
    22.         //Player
    23.         newPlayer.myCamera = newPlayerCamera;
    24.         newPlayer.myPlayerHUD = newPlayerCanvas.GetComponent<PlayerHUD>();
    25.         //Canvas
    26.         newPlayerCanvas.GetComponent<Canvas>().worldCamera = newPlayerUICamera;
    27.         //CameraBase
    28.         newPlayerCamera.myPlayerMov = newPlayer;
    29.         newPlayerCamera.myPlayer = newPlayer.transform;
    30.         newPlayerCamera.cameraFollowObj = newPlayer.cameraFollow;
    31.  
    32.         for (int i=0; i < allPlayers.Length; i++)
    33.         {
    34.             if (allPlayers[i] == null)
    35.             {
    36.                 allPlayers[i] = newPlayer;
    37.                 allCanvas[i] = newPlayerCanvas;
    38.                 allCameraBases[i] = newPlayerCamera;
    39.                 allUICameras[i] = newPlayerUICamera;
    40.                 return;
    41.             }
    42.         }
    43.     }
    The error is with the Instantiate lines:

    Cannot instantiate objects with a parent which is persistent. New object will be created without a parent.
    UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion, Transform)

    The problem persists even using different overloads of the Instantiate method, like I'm using.

    I can't understand what does it mean with persistent parent, since the parents I'm giving them are just normal empty objects. Maybe the problem is with the prefab I'm giving?

    Edit: Solved, I was asisgning the prefabs as parents and not the objects inside the scene, silly me.
     
    Last edited: Jan 27, 2019
    luixodev likes this.
  2. Rayeloy

    Rayeloy

    Joined:
    Feb 12, 2017
    Posts:
    45
    Quick update: The parents are prefabs. I tried using non prefab empty objects and it worked. Why?
     
    funnyhackers6, Kjurek and luixodev like this.
  3. Rayeloy

    Rayeloy

    Joined:
    Feb 12, 2017
    Posts:
    45
    Another update: just now I figured out I was asigning the prefabs, and not the objects in the scene, as parents. Of course it didn't work. Solved.
     
    Last edited: Jan 31, 2019
  4. thoroc

    thoroc

    Joined:
    Sep 23, 2015
    Posts:
    1
    Check how you have instantiated the parent object?
     
  5. keth14

    keth14

    Joined:
    Sep 29, 2019
    Posts:
    3
    Can You tell me the way, how did you find the instantiated object in the scene and not the prefab. I have just started Unity and cant find a way to get the current gameObject through the script. It always refers to the prefab instead of the object in the scene.
    Would appreciate a lot for the answer.
     
    alwlsdydtjd1 and Rayeloy like this.
  6. Konomira

    Konomira

    Joined:
    Oct 11, 2015
    Posts:
    2
    Late reply but I stumbled across this thread while looking for a solution myself and will post a solution for anyone in the future who might need it.

    when you instantiate an object via script, you can get a reference of the instance.
    GameObject go = Instantiate(prefab);
    then when instantiating a child to that, you would call
    Instantiate(childPrefab,go.transform);
    instead of
    Instantiate(childPrefab, prefab.transform);


    EDIT (For clarification):
    You want to make sure that when you're referring to the instance, it's the spawned instance
     go 
    and not the unspawned prefab
     prefab 
     
    Last edited: Mar 5, 2022
    CodeSmile, zeiksz, MaRKTZ and 7 others like this.
  7. BeansVanH

    BeansVanH

    Joined:
    Dec 11, 2019
    Posts:
    1
    This helped a bunch thanks! (not OP but had the same issue)
     
  8. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, but for anyone that finds this, just because the above reply isn't 100% clear(i didnt really get it, i figured it out on my own haha), the issue, for me anyway, was I was setting the object to a child of the UNSPAWNED object. AKA, the prefab itself, rather than the spawned gameobject

    Edit: Realized i wasn't super clear in my clarification lol, so i added a bit extra.
     
    Last edited: Aug 14, 2023
    Gravesend, Rayeloy and Konomira like this.
  9. Rayeloy

    Rayeloy

    Joined:
    Feb 12, 2017
    Posts:
    45
    I'm sorry my self reply was not clear enough, thank everybody for clarifying this.
     
    Mashimaro7 likes this.