- Home /
Instantiate particles with double if statement
Hello everyone, first time posting, as normally I can find the answers to my questions by searching around here. However, I have a project due Tuesday, and I can't seem to find any help for this issue I'm having.
I'm making a game that involves mixing food into a pot, and depending on what ingredients you put in together, different particle systems will be emitted. As I've been building this up I've tested along the way, and I was able to get the particles to instantiate when I put in only one ingredient using OnCollisionEnter. However, I hit the problem of only being able to enter one argument into that function, so I switched to booleans and if statements instead. I have it set up so that the booleans are switched to true if the pot detects a collision with the corresponding object.
From there, I figured it'd be a very simple matter of checking if any given two booleans were true at the same time with a set of if statements, and then instantiating the desired particle system. However, I can't seem to find a way to get this last step functioning. I've been Googling around and searching the forums for any particle instantiation tips I can find, and I've tried them all, but nothing seems to be working so far. I'm desperate for help, because this game is due in 2 days, and if I can't get this to function, I won't have a game at all!
EDIT: I should mention that I intended to put all of the particle systems into the array declared at the top, and instantiate them by calling on their array number down at the bottom.
Here is the code I've been testing with:
var prefab:GameObject[];
var apple:boolean = false;
var turkey:boolean = false;
function Update ()
{
Shenanigans();
}
function OnCollisionEnter(ingredient:Collision)
{
if(ingredient.gameObject.tag == "Apple")
{
apple = true;
}
if(ingredient.gameObject.tag == "Turkey")
{
turkey = true;
}
}
function OnCollisionExit(ingredient:Collision)
{
if(ingredient.gameObject.tag == "Apple")
{
apple = false;
}
if(ingredient.gameObject.tag == "Turkey")
{
turkey = false;
}
}
function Shenanigans()
{
if(apple && turkey)
{
Instantiate(prefab[0],transform.position,transform.rotation);
yield WaitForSeconds(5);
Destroy(prefab[0]);
return;
}
}
Any help would be deeply appreciated!!
Answer by DaveA · Oct 01, 2012 at 02:29 AM
You may need to include more code but here's my $.02
You would need to check apple and turkey in an Update function, not just on Awake, but you could also do this OnCollisionEnter only.
You could have the particle systems already there, just not active (not emitting) unless/until the collision happens.
(I apologize in advance if this posts twice. I wasn't sure if the first one went through.)
Thank you, that was very helpful! When I moved the call to check the if statements to Update, the particles instantiated.
However, this has given me a new problem: I can't seem to get them to only instantiate once. The particles keep instantiating once every update; what I need is to instantiate once, run through the particles, and then remove it. I tried using a yield and Destroy, but I think I may be using it incorrectly, because it appears to have no effect. I've updated the OP coding to reflect the new changes.
Thank you for any additional help!
Your answer
Follow this Question
Related Questions
How Would I instantiate a particle system onto my obstacle on collision after it being destroyed? 0 Answers
Audio and Particles on Trigger 0 Answers
Spawning individual particles at scene coordinates. 1 Answer
How to Instantiate gameObject where a particle dies 2 Answers
Explosion on contact? 1 Answer