I am trying to add a particle effect to the Rock 3(Clone) as a child as below, using this code below it doesn’t seem to work, anyone got any ideas?
I just get this message
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform) WorldMaker:AddAsteroidToScene(String, Int32, String, Single, Int32, Int32, Int32, Int32, Int32) (at Assets/Scripts/WorldMaker.cs:163) WorldMaker:Start() (at Assets/Scripts/WorldMaker.cs:74)
private void AddAsteroidToScene(string preFabName, int damageAmountValue, string orbitType, float scale,int x,int y,int z,int speed,int decay)
{
GameObject asteroid = Resources.Load(preFabName) as GameObject;
var position = new Vector3(x, y, z);
Instantiate(asteroid, position, Quaternion.identity,transform);
MeshRenderer mr = asteroid.GetComponentInChildren<MeshRenderer>();
mr.transform.localScale = new Vector3(scale, scale, scale);
Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroid.transform);
AsteroidExplosionEffect.transform.parent = asteroid.transform;
var script = GetComponent<RotateAroundThis>();
if (script = null)
{
asteroid.AddComponent<RotateAroundThis>();
}
}
Again, you’re making a similar mistake you’re trying to set the parent of the explosion to the prefab instead of the instance in your scene. Capture the asteroid instance:
var asteroidInstance = Instantiate(asteroid, position, Quaternion.identity,transform);
Then use that for setting the parent of the explosion later: