[Solved] Issue with instantiating a prefab

so, I’m somewhat new to unity, but I can say that I understand how objects and prefabs work. instantiating a prefab should make the prefab into a copy and make it a normal object. however me and a friend are collaborating on a project and we’re working on some UI. we finished the inventory system where it instantiates a list of items for the UI to display what items the player has, but upon trying to do the same for a quest/mail list, it gave us a lot of issues.

the code is simple:

public GameObject _QuestEntryPrefab; // prefab which is assigned in inspector

public void AddQuest( Quest.QuestID ID ) {
        Transform parent = transform;
        GameObject newEntry = Instantiate( _QuestEntryPrefab );
        newEntry.GetComponent<MenuQuestMsg>()._QuestTitle = "THIS IS A TEST!!";
    // this code ^ works perfectly fine
        //newEntry.transform.SetParent( parent );
    // this code ^ creates an error.
        _QuestLogList.Add( newEntry );
}

the interesting bit is how editing the object and attached script, works without a problem. but upon trying to set the parent it throws an error depending on how I set the parent.

if we set the parent like this:
newEntry.transform.SetParent( parent );
then it throws this error:
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'QuestLogEntry(Clone)').

while setting the parent like this:
GameObject newEntry = Instantiate( _QuestEntryPrefab, parent );
gives this error:
Cannot instantiate objects with a parent which is persistent. New object will be created without a parent.
the canvas parent is not a ‘Don’tDestroyOnLoad’ object, so I don’t know why it would say the parent is ‘pesistent’.

We’ve tried fixing this by:

  • restarting unity.

  • deleting the prefab and script and making a new prefab/script.

  • varying how the object is instantiated by changing it to

GameObject newEntry = GameObject.Instantiate( _QuestEntryPrefab, parent );


- ```
GameObject newEntry = Instantiate( _QuestEntryPrefab, parent ) as GameObject;

GameObject newEntry = GameObject.Instantiate( _QuestEntryPrefab, parent ) as GameObject;


- changing the parent to something other than the current transform
- none of these worked or even changed the error.

This issue has gone on for ~2 days now of trying to fix, which frustrates us because this is near identical to how we did the inventory UI system, and yet we never got any errors from that?

I’m not sure I understand what is going on but…

you could try setting the parent of the instantiated object like this:

GameObject newEntry = Intantiate(_QuestEntryPrefab); // or (_QuestEntryPrefab, parent.transform.position, parent.transform.rotation) depending on what you're doing.

newEntry.transform.parent = parent.transform; // << this is what really matters

I’m sorry if I didn’t understand your question. Hope it helps!!

no problem! we ended up finding out the issue, it turns out it was because we were referencing the script from the gameManager which was persistent, moving the references to the parent of the canvas which wasn’t fixed the errors.

1 Like