Adding a partical effect to an object

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)

6109268--665144--upload_2020-7-20_15-2-20.png

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>();
        }       


    }

You’re trying to set the parent of the prefab instead of the instance you just created from the prefab. Try this:

        GameObject explosionInstance = Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroid.transform);
                  
        explosionInstance.transform.parent = asteroid.transform;

Hi, thanks for the response, that gets rid of the error but it still doesn’t add it to the scene for some reason

Are you sure? Can you show a screenshot of the hierarchy after adding the asteroid?

Does the explosion destroy itself shortly after being created possibly?

The explosion does not destroy itself

6109868--665207--upload_2020-7-20_17-21-44.png

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:

        GameObject explosionInstance = Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroidInstance.transform);

Then get rid of this line completely because Instantiate in that form will already set the parent properly:

explosionInstance.transform.parent = asteroid.transform;
1 Like

That worked , many thanks mate.