'Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption

I checked this code with a friend that has a better knowledge of Unity, but we can't find the problem. Basically, Unity says that i can't parent a prefab, but i'm trying to change the parent of an instanced object, not the prefab.

I can't understand the error (but I think that it's in the Update method)

public GameObject[] Weapons;
public float projectileSpeed;
public float bulletTime;
public Rigidbody bullet;

private bool canShoot = true;
private float t = 0f;
private int actualBullets;
private GameObject actualWeapon;


private void Update()
{
    t += Time.deltaTime;
    if (actualWeapon != null)
        return;
    actualWeapon = GameObject.Instantiate(Weapons[0], gunPosition.position, gunPosition.rotation) as GameObject;
    actualWeapon.transform.parent = GameManager.instance.player.transform;      
}
public virtual void Fire() 
{
    if (canShoot)
    {
        actualBullets--;
        var nBullet = GameObject.Instantiate(bullet, bulletSpawn.position, Quaternion.identity) as Rigidbody;
        
        nBullet.AddForce (new Vector3(Vector3.forward.x, Vector3.forward.y, projectileSpeed));
        canShoot = false;
    }
    else if (t > bulletTime)
        (canShoot, t) = (true, 0);
}


Solution 1:[1]

Your problem is that

actualWeapon.transform.parent = GameManager.instance.player.transform;

is trying to get a parent that is not instantiated. You need to go to the GameManager and actually instantiate the player GameObject. Then you can keep a reference to the Player and make weapons children of the Player.

Solution 2:[2]

In my case, when developing Glitch Garden in Unity 5.5

private void Fire (){
    GameObject newProjectile = Instantiate(projectile) as GameObject;
    newProjectile.transform.parent = projectileParent.transform;
    newProjectile.transform.position = gun.transform.position;
}

solved by Instantiate prefab projectileParent:

private void Fire (){
        GameObject newProjectile = Instantiate(projectile) as GameObject;
        newProjectile.transform.parent = Instantiate(projectileParent).transform;
        newProjectile.transform.position = gun.transform.position;
    }

Then, It caused another problem that is multiple projectiles. Finally, I removed that line and it worked with following code:

private void Fire (){
    GameObject newProjectile = Instantiate(projectile) as GameObject;
    newProjectile.transform.position = gun.transform.position;
}

Above solution is a temp solution. Finally next video it will be fixed and current code is as follows:

public class Shooter : MonoBehaviour {

    public GameObject projectile;
    public GameObject gun;

    private GameObject projectileParent;

    void Start ()
    {
        projectileParent = GameObject.Find ("Projectiles");

        if (!projectileParent) {
            projectileParent = new GameObject();
            projectileParent.name = "Projectiles";
        }
    }

    private void Fire (){
        GameObject newProjectile = Instantiate(projectile) as GameObject;
        newProjectile.transform.parent = projectileParent.transform;
        newProjectile.transform.position = gun.transform.position;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 apxcode
Solution 2