Solving the JavaScript paradox of: if(x equal true){x becomes false;} if(x equals false){x becomes true;}. Beginner question.
Hi, I've just started using Unity and this is my first question. I searched for similar question using keywords like "true", "false", and "collision", but none of the treads answered my question, so please allow me to ask here.
I am trying to write a JavaScript code, attached to a Sphere, that besides other processes, recognizes when the sphere hits a Cube. When it does, the cubes tagged "Player" emits particles. If it is already emitting, it pauses, and if it is paused, it emits again. The script is as follows (I believe "function OnCollisionEnter(collision:Collision)" is the only part relevant, but I will post the whole script just to be sure):
#pragma strict
function FixedUpdate (){
var x:float = Input.GetAxis("Horizontal");
var y:float = Input.GetAxis("Vertical");
rigidbody.AddForce(x, 0, y);
}
function Start(){
var Cube:GameObject = gameObject.Find('Cube');
var particle:ParticleSystem = Cube.gameObject.GetComponent('ParticleSystem');
particle.Play();
}
function OnCollisionEnter (collision:Collision){
if (collision.gameObject.tag == "Player"){
var halo:Behaviour = collision.gameObject.GetComponent('Halo');
halo.enabled = true;
var particle:ParticleSystem = collision.gameObject.GetComponent('ParticleSystem');
if(particle.isPaused == false){particle.Pause();}
if(particle.isPaused == true){particle.Play();}
}
}
function OnCollisionExit (collision:Collision){
if (collision.gameObject.tag == "Player"){
var halo:Behaviour = collision.gameObject.GetComponent('Halo');
halo.enabled = false;
}
}
I assume since Unity reads the script from top to bottom, after pausing the particle emission, isPaused becomes true, triggering particle.Play(); which means the particle emission won't pause at all.
How can I make unity recognize whether or not the particle emission is paused at collision time, and act accordingly? Or any other solution is fine as well.
Thank you for your time.
Answer by springwater · Oct 23, 2015 at 03:50 PM
@GFRibeiro, The logic might be like this, assuming the player gets hit by a fireball for example and emits fire particles. "else" is the key word to straighten things out in these cases in my experience.
OnCollisionEnter (collision:Collision){
if (particle.isPaused == true)
particle.Play();
else
particle.Pause();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612042422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My method is called more than once. 1 Answer
How to save Component 1 Answer
Animation Problem 0 Answers
script enable=false 3 Answers