Under Attack - Error message

Hope someone can help I keep getting this error message Setting the parent of a transform which resides in a prefab asset is disabled to prevent data corruption (GameObject:“EnimiesGoBoom sparks(Clone)”)

Code is
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour

{

[SerializeField] GameObject deathVFX;

[SerializeField] GameObject hitVFX;

[SerializeField] Transform parent;

[SerializeField] int scorePerHit = 15;

[SerializeField] int hitPoints = 4;

ScoreBoard scoreBoard;

void Start()

{

    scoreBoard = FindObjectOfType<ScoreBoard>();

   

    AddRigidbody();

}

void AddRigidbody()

{

    Rigidbody rb = gameObject.AddComponent<Rigidbody>();

    rb.useGravity = false;

}

void OnParticleCollision(GameObject other)

{

    ProcessHit();

    if (hitPoints < 1)

    {

        KillEnemy();

    }

}

void ProcessHit()

{

    GameObject vfx = Instantiate (hitVFX, transform.position, Quaternion.identity);

    vfx.transform.parent = parent;

    hitPoints--;

    scoreBoard.IncreaseScore(scorePerHit);

}

void KillEnemy()

{

    GameObject vfx = Instantiate(deathVFX, transform.position, Quaternion.identity);

    vfx.transform.parent = parent;

    Destroy(gameObject);

}

}

1 Like

The object you are instantiating is a child of another object and they’re all in a prefab. You cannot set the transform on it because it will pull it out of the prefab which, as the error states, is not allowed. You could fix it by not having it inside the prefab (from your screenshot it looks like it doesn’t need to be there) and referencing it directly from the project tab.

I am, of course, somewhat guessing here. I cannot see the prefab, but I can see that you have AliceGoBoom inside the player prefab, so I am assuming you have a similar setup for enemies

1 Like

This is a very, very weird bug. It doesn’t matter if you are trying to change the parent of an instantiated prefab, an instantiated child of a prefab, an instantiated nested prefab, or whatever, it should not throw that error, I tried everything.

The only way to replicate that bug is by trying to change the parent of the prefab itself, not the instantiated prefab:

    [SerializeField] GameObject cylinderPrefab;
    [SerializeField] Transform parent;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            cylinderPrefab.transform.parent = parent;
        }
    }

I’m not seeing that anywhere in your code.

I suppose these might be the causes for that bug to happen.

  • You have a script that is doing that and you forgot to delete it. (Or you duplicated it without even noticing, that’s a fairly common mistake)
  • The error is coming from somewhere that isn’t the script you showed us.
  • You forgot to save your script.
  • Unity issues, try restarting.

Hope this helps.

2 Likes

Thanks for the help guys found the issue. I created a enemy from a prefab and didnt save it as its own that was creating the clone.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.