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

Hello!

I am instantiating rigidbody gameobjects, I am trying to get the to be a child of a parent gameobject that is already within the scene.

I found an answer on this old post here:

The only this is that my objects are rigidbodies, not just game objects. so typing “as Gameobject” does not work.

Here is my current code:

{

                Rigidbody clone;
                clone = Instantiate(Proton, new Vector3(line.xCoord / 32, line.yCoord / 32, line.zCoord / 32), Quaternion.Euler(0, 0, 0));

                clone.velocity = transform.TransformDirection(new Vector3(line.Px / 64, line.Py / 64, line.Pz / 64));

            }

Is there a way I can instantiate these objects as Gameobjects in order to make them a child of a parent within the scene while keeping their rigidbody aspects? Thanks!

I don’t think that’s the line of code that’s giving you the error. When you get an error, it will have two numbers in parentheses at the beginning, that is the line and column of the error. You can also double click on the error and it will bring you to the line. I’m guessing you’re trying to set the transform of the prefab instead of setting the transform of the thing you’re instantiating, just like the error says.

I’m curious why you are instantiating them as rigidbodies instead of instantiating them as gameobjects and simply accessing their rigidbody components.

I should probably add… you can’t actually “instantiate a rigidbody”. You can only instantiate GameObjects. What you’re doing with that Instantiate call is instantiating a game object that is a copy of the prefab, and then getting its attached Rigidbody. You can get the gameObject with clone.gameObject.

Thank you for your replies! I have updated the code and it seems to be working. Here is the update code.

GameObject clone = Instantiate(Proton.gameObject, new Vector3(line.xCoord / 32, line.yCoord / 32, line.zCoord / 32), Quaternion.Euler(0, 0, 0)) as GameObject;

                clone.GetComponent<Rigidbody>().velocity = transform.TransformDirection(new Vector3(line.Px / 64, line.Py / 64, line.Pz / 64));

Though, I am still getting the error. My “clone” object has a child gameobject that has a trail renderer creating a trail behind this moving particle. If the “clone” is instantiated as a gameobject and not a prefab, is the child also created as a gameobject? If not, I am getting this error from the child.

When this “clone” is destroyed, the parent of the child object switches in order to keep the trail rendered within the scene.
Here is the code for the switch:

public TrailRenderer trailer;
public GameObject Parent;

void OnDestroy()
    {
        trailer.transform.parent = Parent.transform;
}

Currently, the trails are being destroyed when the “clones” are destroyed instead of switching parents.

As I said, if you see an error in the console, it will have the line number giving you the error in it, and you can double-click it to jump to the line with the error. Do that, and post the actual line that is throwing the error, then it will be much easier for people to help you.

public TrailRenderer trailer;
    public GameObject Parent;
   

    void OnDestroy()
    {
        trailer.transform.parent = Parent.transform; //console says this is causing the error
    }

The Gameobject “trailer” is the prefab child of the Proton parent. I made an empty Gameobject within the scene called “Trailer Parent” that “trailer” would then become its child after the destruction of the Proton through this code. Even though I am now instantiating a “Proton” as a Gameobject, I am still getting the error (the title of this post), which I am now assuming it’s from the child “trailer”.

Like the error said, you can’t set the parent of a prefab. “trailer” is a prefab, so you can’t set its parent. It doesn’t matter what you did with “Proton” since that’s nowhere in this code and you’re not setting its parent. You’re setting the parent of “trailer”, which is a prefab. You can’t do that. Maybe what you want to do is Instantiate a copy of “trailer” and then set the parent of that.

Super Old But:

Like MakeShiftWings says “Instantiate a copy of X(your game object)” which is given reference in scene. You are technically referencing nothing in the scene.

    public TrailRenderer trailer;
        public GameObject Parent;
     
   
        void OnDestroy()
        {
           GameObject trailerCopy = Instantiate(topicButton_Prefab);
             // Instantiate() - has parameters as well
            trailerCopy .transform.parent = Parent.transform; 
//use the new created copy (which is in scene) for the new transform, that's what you wanted anyways
        }
1 Like