0

I have a character that is going to collide with a coin. When the character collides with the coin, a particle "animation" should occur. Here's my code so far. Some basic assistance would help a lot. This code is attached to the player character.

    void OnTriggerEnter(Collider _hit)
{
    if (_hit.tag == "Coin")
    {
        Destroy(_hit.gameObject);
        coinCount++;
        coinsText.text = "Coins: " + coinCount.ToString() + "/" + coinTotal.ToString();
        var Bling : GameObject = Instantiate(Bling, transform.position, Quaternion.identity);
    }
}
AppKing
  • 3
  • 2
  • That can't be "vanilla C#" as such I'm not sure the C# tag is appropriate. In particular, this `var Bling : GameObject = ...` is not legal C# syntax. – Lasse V. Karlsen May 16 '15 at 22:34
  • Bling is basically the GameObject that automatically plays (and auto-deletes) the particle "animation". I am basically trying to spawn this in the same place as the player character. – AppKing May 16 '15 at 23:16
  • I'm pretty sure he's copying a Java example not realizing that he's looking at a Java example? Unity accepts both Java and C# and, as such, there are multiple examples from both languages floating around the internet. "var Bling : GameObject" is Javascript syntax. e.g.: http://answers.unity3d.com/questions/237217/pragma-strict-and-getcomponent.html – ThisHandleNotInUse May 17 '15 at 04:53

1 Answers1

1

This is what you need to do.

public ParticleSystem collisionParticlePrefab; //Assign the Particle from the Editor (You can do this from code too)
private ParticleSystem tempCollisionParticle;

void OnTriggerEnter (Collider _hit)
{
    if (_hit.tag == "Coin") {
        Destroy (_hit.gameObject);
        coinCount++;
        coinsText.text = "Coins: " + coinCount.ToString() + "/" + coinTotal.ToString();
        tempCollisionParticle = Instantiate (collisionParticlePrefab, transform.position, Quaternion.identity) as ParticleSystem;
        tempCollisionParticle.Play ();
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I am getting one issue! When the game changes to the next level (level 1 to level 2 for example) then it doens't play the particle animation and gives an error like "ArgumentException: The thing you want to instantiate is null". Any ideas how to solve this? – AppKing May 18 '15 at 11:43
  • Your prefab is null in your seconds scene. Add the particle emit prefab to your collisionParticlePrefab. You can also do this from code before if (_hit.tag == "Coin") { with collisionParticlePrefab = GameObject.Find("nameOfGameObjectPArticleIsAttachedTo").getComponent(); – Programmer May 18 '15 at 12:16