Set Parent Problem

Hi.

I wrote below code for set Instantiated prefab to parent of the player. but it don’t work and give me below error.

error: " Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FallingShieldItem : MonoBehaviour
{

    public GameObject PlayerSheild;
    public GameObject player;
  
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            GameObject SheildObj = Instantiate(PlayerSheild, player.transform.position, Quaternion.identity);
            SheildObj.transform.parent = player.transform;
            Destroy(gameObject);         
        }
    }
}

while I used this code for set Instantiated prefab to player parent.

SheildObj.transform.parent = player.transform;

can anyone tell me why unity give me this error?

Thanks.

OK, I find the problem:

In code I should use GameObject.FindWithTag(“Player”) instead public attached prefab as Gameobject.

So code should be change like below that work very well :

            GameObject SheildObj = Instantiate(PlayerSheild, GameObject.FindWithTag("Player").transform.position , Quaternion.identity);
            SheildObj.transform.parent = GameObject.FindWithTag("Player").transform;