InvalidCastException when using Instantiate on a ParticleSystem

Hi all,

I’m using Unity 5.5.1f1 Personal Edition. In my script, I declared a ParticleSystem variable:

public ParticleSystem SmokeParticle;

and I’m assigning it from the editor. Then in the script I have:

var smokeEffect = Instantiate<ParticleSystem>(SmokeParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));

At runtime, every time this instruction is hit, I get the following exception in the console:

InvalidCastException: Cannot cast from source type to destination type.
UnityEngine.Object.Instantiate[ParticleSystem] (UnityEngine.ParticleSystem original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:207)

I have this problem since I updated to Unity version 5.5.1f1, the previous version worked fine with this.

Can someone please explain me what I’m doing wrong?

Thank you all,

FOC

Hi Commoble, thank you for your time. Here is the rest of the stack trace from the console:

InvalidCastException: Cannot cast from source type to destination type.
UnityEngine.Object.Instantiate[ParticleSystem] (UnityEngine.ParticleSystem original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:207)
WeaponController.ApplyHole (RaycastHit hit) (at Assets/Scripts/WeaponController.cs:313)
WeaponController.Update () (at Assets/Scripts/WeaponController.cs:184)

Line 313 of WaponController.cs is this one:

 var smokeEffect = Instantiate<ParticleSystem>(SmokeParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));

I alredy tried 2) as I know sometimes the keyword var can lead to some ambiguous behaviour, but no luck.

I checked the parameters:

  • SmokeParticle is of type ParticleSystem;
  • hit.Point is of type Vector3;
  • Quaternion.FromToRotation gives a Quaternion type

the signature of the generic method is this:

Instantiate<ParticleSystem>(ParticleSystem a, Vector3 b, Quaternion c)

so everything looks fine to me.

The whole method is:

private void ApplyHole(RaycastHit hit)
    {
        var hole = Instantiate(HoleTexture[0], hit.point, Quaternion.FromToRotation(-Vector3.forward, hit.normal)) as GameObject;

        if (hit.rigidbody != null)
            hole.transform.parent = hit.rigidbody.transform;

        Destroy(hole, 8);

        ParticleSystem smokeEffect = Instantiate(SmokeParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));

        Destroy(smokeEffect, 2);
    }

Do you need the Update routine code?

FOC

Try instantiate the prefab and move/rotate the instance separately instead of handling all actions in instantiate.

Hi all, I had an answer from the unity staff: this is not a bug.

"Thanks for reporting the issue. This is not a bug of Unity though.

You should use GameObject type for a variable which you are using in Instantiate method if you are instantiating from a prefab. Changing line 66 to “public GameObject SmokeParticle;” and line 313 to “GameObject smokeEffect = Instantiate(SmokeParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));” should fix the issue."

I’m going to mark this as the solution.
Thank you.