Instantiating as a child error

void BubbleCome() {
Instantiate(bubble, new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z + 1), Quaternion.Euler(new Vector3(38, 0, 0)));
bubble.transform.parent = transform;
Invoke(“BubbleCome”, Random.Range(repeatMin, repeatMax));
}

The idea is that the continuously instantiated object (bubble) becomes a child of the game object to which the script is attached to pretty much as soon as it spawns. But the problem is that unity gives me an error:

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

Any idea why this happens and how I could fix it?
Other than this function the only other one is the “BubbleCome();” set in the Start function.

Thanks! :slight_smile:

I think you should create a GameObject pointer to instantiate it. such as:

GameObject newBubble = (GameObject)GameObject.Instantiate(bubble, new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z + 1), Quaternion.Euler(new Vector3(38, 0, 0)));

Then:

newBubble.transform.parent = transform;