Search Unity

Instantiate() not returning a handle to use created GameObject

Discussion in 'Scripting' started by SoftwareGeezers, May 24, 2015.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Basic stuff not working. Trying to destroy a particle system spot effect. Create it instantiating a GO added in the inspector:

    Code (CSharp):
    1. GameObject instantiatedGO;
    2.  
    3. public void FX_KilledPuff(Vector3 pos){
    4.     instantiatedGO = Instantiate (particlesKill, pos, Quaternion.identity) as GameObject;
    5.     print ("Instantiated name "+instantiatedGO.name);
    6.     instantiatedGO.name = "ertfg";
    7.     Destroy (instantiatedGO, 1.0f);
    8. }
    When this is called, the particle system is created but 'instantiatedGO' is null.
     
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Just fixed. Leaving this here in case anyone else hits the same problem. Passing Quaternion.identity as per the docs successfully creates the object but fails to return an object. Use source.transform.rotation (or whatever rotation) instead and the reference handle works.

    Code (CSharp):
    1.     instantiatedGO = Instantiate (particlesKill, pos, particlesKill.transform.rotation) as GameObject;