- Home /
Spawning Particle Effects From Code Does Nothing
I made a Squirtle and I want to make it do a Bubblebeam attack that starts and continues for a few seconds, then gets destroyed, then has a wait before it can be done again. I want the Bubblebeam to spawn from his tongue, which is a separate part of the mesh. And the Bubblebeam is a particle effect. My code goes like this:
Heading
static function UseBubblebeam (Tongue : GameObject,
BubblebeamPrefab : GameObject,
Bubblebeam : Transform) {
if (Input.GetKey("[1]")){
var BubblebeamAttack : GameObject = Instantiate (BubblebeamPrefab, Tongue.transform.position, Tongue.transform.rotation);
yield WaitForSeconds(3.0);
Destroy (Bubblebeam);
yield WaitForSeconds(5.0);
}
}
There are no error messages but when I press the button nothing happens... Does this have to do with the input? (and I have tried both the numPad and the row at the top of the keyboard)
Answer by aldonaletto · Jul 09, 2011 at 02:57 AM
You're checking for Input.GetKey inside the function UseBubblebeam. You should check for input in Update, since it's called each frame.
EDITED AGAIN: Answer edited to destroy the instantiated object after 3 seconds, and to include a reload time of 5 seconds:
var Tongue : GameObject;
var BubblebeamPrefab : GameObject;
var nextShot : float = 0;
function UseBubblebeam () {
var BubblebeamAttack : GameObject = Instantiate (BubblebeamPrefab, Tongue.transform.position, Tongue.transform.rotation);
Destroy (BubblebeamAttack, 3); // destroy automatically after 3 seconds
}
function Update(){
if (Time.time>nextShot && Input.GetKeyDown("[1]")){
UseBubblebeam();
nextShot = Time.time + 5; // next shot in 5 seconds
}
}
You must drag the objects you want to the variables Tongue and BubblebeamPrefab. When you press 1 in the keypad, BubblebeamPrefab will be instantiated at Tongue position, and will be destroyed automatically after 3 seconds. The reload time was set to 5 seconds.
@aldonaletto Your note is correct. However, that code brings the error message "Destroying assets is not permitted to avoid data loss." I don't want the actual prefab in the project view destroyed, but in the code I want an object to be created and last for three seconds (the empty mesh for the particle effect), and then for the mesh (not the prefab from the project view) to get destroyed and have a reload time of five seconds. How might I do that? (BTW I'm a total noob at javascript in case you haven't noticed..)
I edited the answer to create the BubblebeamAttack and destroy it after 3 seconds. But what about the mesh you've mentioned? Is it the Tongue? You must do the same thing you've did with BubblebeamPrefab: instantiate it at certain position, then destroy it after some time. You can also use Destroy(object, delay), and the object will automatically be deleted after the specified delay.
Well the tongue is where I want it to spawn. I don't want the tongue to be destroyed which hasn't happened with the recent revisions. But now when I try to press the 1 key again, nothing happens even if I remove the second yield. Could that be because the variable gets destroyed in the script?
In the script above, only the instance created is destroyed. The prefab will be kept alive and safe. It should work anytime you pressed the keypad key 1.
I edited the answer again to include the reload time. It's 5 seconds from the last shot. If you want it to be 5 seconds after the end of the last shot, raise the time to 8 in the last line.
Answer by xbennettx821 · Jul 12, 2011 at 05:57 PM
The BubblebeamPrefab is an empty mesh with a particle system attatched. I'm starting to think it would've been smarter to just make the particle system on the tongue and make the emitter a boo. I'll try experimenting with that but thanks a bundle for the help :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612135642im_/https://answers.unity.com/themes/thub/images/avi.jpg)