x


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)

more ▼

asked Jul 09, 2011 at 02:03 AM

xbennettx821 gravatar image

xbennettx821
1 7 6 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 09, 2011 at 02:57 AM

aldonaletto gravatar image

aldonaletto
57.2k 31 79 292

@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..)

Jul 10, 2011 at 06:29 PM xbennettx821

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.

Jul 10, 2011 at 07:53 PM aldonaletto

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?

Jul 10, 2011 at 10:17 PM xbennettx821

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.

Jul 10, 2011 at 10:34 PM aldonaletto

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.

Jul 10, 2011 at 10:42 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 12, 2011 at 05:57 PM

xbennettx821 gravatar image

xbennettx821
1 7 6 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1215
x767
x560
x9

asked: Jul 09, 2011 at 02:03 AM

Seen: 2941 times

Last Updated: Jul 12, 2011 at 05:57 PM