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.
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.
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
}