• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by derobile42 · May 09, 2020 at 01:18 PM · instantiate2d gameparticle system

Instantiated Particle System seems to only be playing randomly and isn't being destroyed when it finishes.

I'm trying to get a particle effect to spawn when a bullet collides with a solid object, and then get destroyed after finishing. I've managed to the first part, but the thing is, it appears the particle effect is either playing fully, partially, or not at all. And as for being destroyed, that isn't happening.

Here's a video of it:

https://vimeo.com/416648050

As you can see the effect does trigger, but only sometimes.

Here's the code for the bullet:

 using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     //bullet stuff
     public float speed;
     public int damage;
 
     //how far the raycast shoots
     public float distance;
     //determines what layers the bullet will collide and therefore stop on
     public LayerMask WhatIsSolid;
 
     //partcile effect
     public GameObject ps;
 
     void Update()
     {
         //Moves the bullet forwards
         transform.Translate(Vector2.right * speed * Time.deltaTime);
 
         //Shoots a raycast forward at the bullets positition that reaches the speciied distance
         //and only collides with the specified layers. It then assigns this information to an item called "hitInfo"
         RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, WhatIsSolid);
 
         //If the hitInfo collider is not empty or null
         if (hitInfo.collider != null)
         {
             if (hitInfo.collider.CompareTag("Slime"))
             {
                 //calls the TakeDamage function from the Script on the slime enemy
                 hitInfo.collider.GetComponent<EnemyController>().TakeDamage(damage);
             }
 
             StartCoroutine(bulletEffect());
 
             //destroys the bullet game object
             Destroy(gameObject);
         }
     }
 
     IEnumerator bulletEffect()
     {
         print("Effect should play");
         //should play the bullet effect at the bullets position 
         GameObject effect = (GameObject)Instantiate(ps, GameObject.Find("EffectSpawnPos").transform.position, Quaternion.identity);
         //waits for the effect to finish
         yield return new WaitForSeconds(0.3f);
         //supposed to destroy the effect that has now finished 
         Destroy(effect.gameObject);
         print("Effect should die");
     }
 
 }

And here's the Particle System settings: alt text

alt text

That's all I'd imagine might be causing the problem but I'll be happy to give more if need be.

Cheers

005b-500x500.png (50.7 kB)
untitled.png (39.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ParticleSystem showing in scene but not in game. 7 Answers

Instantiating randomly sized prefab to the right of another 2D 0 Answers

Instantiate GameObject at Random time is not working 1 Answer

Spawn: Instantiate GameObject 1 Answer

Instantiate a partical system 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges