Error on parent transform

Hey

When I drag a prefabed Gnome or Cactus in the scene, there “weapons” don’t appear under Projectiles in the Hierarchy.
Also I get this error message: Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

I draged prefabs to prefabs as Ben said so I don’t know what’s wrong.
How can I solve this problem, I’m using the code of the course:

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

I solved it with this code:

    public GameObject projectile, 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;
}
2 Likes

Privacy & Terms