Particle System is not playing through code - c#

I'm using this method and tried some others as well but unable to play the particle system(i.e for bullet shells ).
public ParticleSystem particleSystem;
void Start() {
particleSystem = GetComponent<ParticleSystem>();
}
void Update() {
particleSystem.Play();
}

Do not put your play triggers in the Update() of a MonoBehaviour because it will call play on every frame.
Try something like this instead:
public ParticleSystem particleSystem;
private bool isPlaying = false;
void Start() {
particleSystem = GetComponent<ParticleSystem>();
particleSystem.Clear(); // Reset the particles
}
void Update() {
if(!isPlaying) {
particleSystem.Play();
isPlaying = true;
}
}
Using particleSystem.Clear() before calling particleSystem.Play() can also help with particle issues.
The easiest way to stop and play a particle system is to deactivate and activate the particle system game object. To do this, the controlling script must be placed in a parent or separate object.

You have to make sure it's not already playing, or you will reset it
void Update() {
if(!particleSystem.isPlaying) particleSystem.Play();
}

okay so after trouble of hours i found that the best way to manipulate the particle system i.e activate and detivate on your will is to create an empty gameobject assign the particle system you made to it and then on and off this gameobject exactly at which part in the code you want to by setActivce command. I hope it will help someone in someway

Related

Why can't I "unhide" my gameobject in unity by using SetActive(true)

I am making a little game and I want my character to disappear until you press space, but I don't want to destroy it since it has a script inside. I tried what I wrote below, but it didn't work.
void Start () {
gameObject.SetActive(false);
}
void Update () {
if (Input.GetKeyDown(Keycode.Space)) {
gameObject.SetActive(true);
}
Have any clues to fix it or replacement code?
You set the GameObject to disabled with SetActive(false). As a result, the Update() method is no longer executing. Because of this, the code that will unhide your character can never run.
There are many ways to get the behavior you want, but the simplest I can think of is this: Instead of disabling the entire gameobject, just disable the renderer (whether it's a sprite renderer or mesh renderer). This will make your character disappear, but this script will keep running.
Example:
public Renderer renderer; // drag your renderer component here in the Unity inspector
void Start ()
{
renderer.enabled = false;
}
void Update ()
{
if (Input.GetKeyDown(Keycode.Space))
renderer.enabled = true;
}
Another approach (and I think, better) is to create a child object of your character's GameObject, call it "Body", and place everything that deals with rendering your character in there. Then disable/enable that child gameobject as desired.
Using gameObject.SetActive(false) disables the gameObject and all its components, including the script you're running so it can't turn itself on if the script driving it is off.
If you add the script to a parent object of the object you're trying to disable you can get the effect you're looking for like this:
public gameObejct childObject;
void Start()
{
childObject.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(Keycode.Space))
{
childObject.SetActive(true);
}
}

Sound is not playing on collision

I'm working on a Game with Unity and now i want to implement a sound effect whenever the projectile hits an object. Somehow there is no sound playing. Here is the Code:
AudioSource sound;
void Start()
{
...
sound = GetComponent<AudioSource>();
...
}
void Destroy()
{
sound.Play();
}
You have not shown if Destroy() function is called or not. Please add a Debug.Log("Called") inside your Destroy() function and test it. If it is getting called, it could be related to the volume of the system or if the Audio component is in mute. Please check these initial things.
If it is not called then learn the difference between GetComponents.
When you attach the script to your gameobject that contains AudioComponent, write it this way:
private AudioSource sound;
void Start()
{
sound = GetComponent<AudioSource>();
sound.Play();
}
When you attach the script to a different gameObject and would like to reference the Audio gameObject to this script, write it this way:
public AudioSource sound;
void Start()
{
sound.GetComponent<AudioSource>(); //this is optional
sound.Play();
}
Don't forget to add it as reference (Drag & Drop Audio component's GameObject) in the second solution.

How to activated object again in unity

I created a dot following my mouse around in 2D and I created a cube object changing position on x and y. Now when I point my mouse to cube, it deactivates I set that, and now I want to activate it again. I try on trigger exit, but it didn't work.
public GameObject tapObject;
private float respawnTime = 1f;
public float xMin;
public float xMax;
public float yMin;
public float yMax;
void Start()
{
StartCoroutine(spawnEnemyTime());
}
private void RandomSpawnObject()
{
tapObject.transform.position = new Vector2(Random.Range(xMin, xMax), Random.Range(yMin, yMax));
}
private void OnTriggerEnter2D(Collider2D collision)
{
tapObject.SetActive(false);
}
IEnumerator spawnEnemyTime()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
RandomSpawnObject();
}
}
Once inactive the scripts on that object are not executed anymore => messages like OnTriggerExit are not called/executed.
One solution is to simply wrap the target object in a parent object and attach your script to the parent instead but make it (de)activate the child.
So the parent stays active and receives the message.
I am just going to repeat what everyone else here said:
A inactive object in Unity is truly inactive, meaning it does not receive any updates, can't collide with stuff and all the MonoBehaviour stuff that usually calls your code does not work either. You have to manually re-activate the object using a reference that you cached somewhere.
But, instead of just flat out disabling the whole object you could disable the components that you don't want to be active.
Example:
private void OnTriggerEnter2D(Collider2D collision)
{
tapObject.GetComponent<Renderer>().enabled = false;
}
private void OnTriggerExit2D(Collider2D collision)
{
tapObject.GetComponent<Renderer>().enabled = true;
}
This only deactivates your renderer component but leaves everything else as it is. So your object can still collide and it's still registered via e.g. OnTriggerExit.
Keep in mind that GetComponent<T>() is a pretty expensive operation so caching your component references is a good idea. The best solution would be to start out with a reference by creating a variable for it and assign it in the inspector.
Example:
//Set in inspector
public Renderer renderer
private void OnTriggerEnter2D(Collider2D collision)
{
renderer.enabled = false;
}
private void OnTriggerExit2D(Collider2D collision)
{
renderer.enabled = true;
}
When a GameObject is not active in Unity , you can't click it(no rendering,no colliding , nothing )
But ,You can create a hotkey (new script or in other script) , that can set it back to active , if it is not active.
public GameObject GO;
Use GO.setactive(true);
whereas gameobject is the object use to define the specific thing or object which needs to be active and the whole code needs to written in the method "spawnEnemyTime" so that it could be get active after the specific time period
You can just use an empty GameObject and get a reference the object that you want to enable/disable. If you get the reference before you disable it you will be able to activate it again.
the alternative is to do what TehMightyPotato said. Disable components it's actually the best way to solve this problem, but if you have lot's of components/subcomponents disable the gameobjects is faster.

Particle System not working on Play()

I have a particle effect that I would like to trigger to play, and then stop. I'm sure this is an easy fix that I am over looking.
The particle can instantiate and play, but this obviously leaves overhead and particles that are active in the hierarchy when they don't need to be.
public void EmitFX(ParticleSystem particle)
{
Instantiate(particle, particlePos, Qauternion.identity)
}
I would like to use methods within ParticleSystem but am running into some problems. I have been using the manual and am still running into a block. I've googled this up and down, based on problems others had I changed my code to the following. It still does not work and is now a monster based on hacks other people found useful :/
public void EmitFX(ParticleSystem particle)
{
particle = particle.GetComponent<ParticleSystem>();
particle.transform.position = ballPos;
var em = particle.emission;
em.enabled = true;
particle.Play();
}
Here is a s/c of a particle in the Inspector.
First of all, not sure what the particle = particle.GetComponent<ParticleSystem>(); line is supposed to do? The particle variable is the ParticleSystem provide to your EmitFX() method so no need to call for this.
My guess is you have some references problems in your script (some variables referring to your prefab, then what you instantiate overrides this reference, ...) so I wrote you a "cleaner" version of your code (by merging your two scripts):
#region Attributes
[SerializeField]
private ParticleSystem particle;
private ParticleSystem generatedParticle;
#endregion
#region MonoBehaviour
protected void Start()
{
generatedParticle = null;
}
protected void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
EmitFX(particle);
}
}
#endregion
public void EmitFX(ParticleSystem a_Particle)
{
if(generatedParticle == null)
{
generatedParticle = Instantiate(particle, particlePos, Qauternion.identity);
}
generatedParticle.transform.position = ballPos;
generatedParticle.Play();
// You can set a fixed duration here if your particle system is looping
// (I assumed it was not so I used the duration of the particle system to detect the end of it)
StartCoroutine(StopFXAfterDelay(generatedParticle.main.duration));
}
private IEnumerator StopFXAfterDelay(float a_Delay)
{
yield return new WaitForSeconds(a_Delay);
generatedParticle.Stop();
}
What it does is it store the instantiated particle in a variable so it can access it later and remember it has been generated. Also I added a coroutine to deactivate it at the end of the effect.
Hope this helps,
I know this is an old question but I am going to place here what worked for me since the problem persists in the later versions of Unity.
Instead of just calling: particle.Play() first stop the particle system like the code below.
particles.Stop();
if (particles.isStopped)
{
particles.Play();
}
Where 'particles' is a component of type "ParticleSystem".

Unity: Can't access Childrens SpriteRenderer.enabled

I have problems accessing the enabled variable in children of my Player GameObject.
-Human holds Rigidbody2D, BoxCollider, PlayerController script and Animator
-body and range_attack_body only hold SpriteRenderer
What I want to do:
I want to change the SpriteRenderer of my Player Object when mouse button is on hold. There are SpriteRenderers in body and range_attack_body. Both GameObjects are part of animations.
body.SpriteRenderer is active and range_attack_body.SpriteRenderer inactive during normal motion.
In my PlayerController script I wrote a routine that will trigger an attack animation when mouse button is on hold. In this routine I wanted to change the enabled states of the SpriteRenderers. However nothing is happening, meaning the varibales are not changing during runtime. I already checked if the GameObjects and SpriteRenderers are accessed correctly during Awake() and I can find both renderers in my SpriteRenderer array using debug messages.
On top of that I checked what happens if I add a SpriteRenderer to my Human GameObject. It will appear in my SpriteRenderer array and I have full access to enabled variable meaning I can change them in my routine. So I figured there might be a conflict with body and range_attack_body due to their SpriteRenderers being part of animations. I added Human.SpriteRenderer to an animation and can still change variables.
I have no clue what is going on, please help. Here is some code:
public class PlayerController2D : PhysicsObject {
public float maxSpeed = 7f;
public float jumpTakeOffSpeed = 7f;
public float posOffset = 1;
protected bool flipSprite = false;
protected bool flipState = true;
private Animator animator;
private SpriteRenderer[] spriteRenderers;
void Awake ()
{
animator = GetComponent<Animator> ();
GameObject human = GameObject.Find("Human");
spriteRenderers = human.GetComponentsInChildren<SpriteRenderer> ();
}
protected override void Attack ()
{
if(Input.GetMouseButton(0))
{
spriteRenderers[0].enabled = false;
spriteRenderers[1].enabled = true;
Debug.LogError("Inhalt:" + spriteRenderers[0].ToString());
Debug.LogError("Inhalt:" + spriteRenderers[1].ToString());
animator.SetBool("attack", true); // boolean to start hold animation
}
else if(!Input.GetMouseButton(0))
{
spriteRenderers[0].enabled = true;
spriteRenderers[1].enabled = false;
animator.SetBool("attack", false);
}
}
}
Your script code is ok, the problem should be elsewhere.
First of all, disable the Animator component and run the script: if enabling/disabling the sprite renderers work, than you must look in the animation clips if you have the Sprite Renderer.Enabled property anywhere, most probably you'll have it - remove it so you can enable/disable the renderers only via script.
If the script still doesn't work, then the problem is elsewhere (another script which is accessing the enabled property of the renderers).
But I'd bet that you're changing the enabled property from the animation clips, which supercedes the script due to how the Unity execution order works (animations are always updated after the scripts and before rendering).

Categories

Resources