Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Adding a partical effect to an object

Discussion in 'Scripting' started by SmartyDrawsStuff, Jul 20, 2020.

  1. SmartyDrawsStuff

    SmartyDrawsStuff

    Joined:
    Dec 15, 2015
    Posts:
    5
    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)



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

    Code (CSharp):
    1. private void AddAsteroidToScene(string preFabName, int damageAmountValue, string orbitType, float scale,int x,int y,int z,int speed,int decay)
    2.     {
    3.         GameObject asteroid = Resources.Load(preFabName) as GameObject;
    4.         var position = new Vector3(x, y, z);
    5.  
    6.         Instantiate(asteroid, position, Quaternion.identity,transform);
    7.            
    8.         MeshRenderer mr = asteroid.GetComponentInChildren<MeshRenderer>();
    9.         mr.transform.localScale = new Vector3(scale, scale, scale);
    10.              
    11.  
    12.         Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroid.transform);
    13.                    
    14.         AsteroidExplosionEffect.transform.parent = asteroid.transform;
    15.    
    16.  
    17.         var script = GetComponent<RotateAroundThis>();
    18.         if (script = null)
    19.         {
    20.             asteroid.AddComponent<RotateAroundThis>();
    21.         }      
    22.  
    23.  
    24.     }
     
    Last edited: Jul 20, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're trying to set the parent of the prefab instead of the instance you just created from the prefab. Try this:

    Code (CSharp):
    1.         GameObject explosionInstance = Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroid.transform);
    2.                  
    3.         explosionInstance.transform.parent = asteroid.transform;
     
  3. SmartyDrawsStuff

    SmartyDrawsStuff

    Joined:
    Dec 15, 2015
    Posts:
    5
    Hi, thanks for the response, that gets rid of the error but it still doesn't add it to the scene for some reason
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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?
     
  5. SmartyDrawsStuff

    SmartyDrawsStuff

    Joined:
    Dec 15, 2015
    Posts:
    5
    The explosion does not destroy itself

    upload_2020-7-20_17-21-44.png
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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:
    Code (CSharp):
    1. var asteroidInstance = Instantiate(asteroid, position, Quaternion.identity,transform);
    Then use that for setting the parent of the explosion later:

    Code (CSharp):
    1.         GameObject explosionInstance = Instantiate(AsteroidExplosionEffect,position, Quaternion.identity, asteroidInstance.transform);
    2.  
    Then get rid of this line completely because Instantiate in that form will already set the parent properly:
    Code (CSharp):
    1. explosionInstance.transform.parent = asteroid.transform;
     
    Joe-Censored likes this.
  7. SmartyDrawsStuff

    SmartyDrawsStuff

    Joined:
    Dec 15, 2015
    Posts:
    5
    That worked , many thanks mate.