x


How do I Update the transform of Instantiate prefab?

I have a prefab Shuriken Particle system & I want it to follow a rock after it collides.
Problems I have with this 1.I can get the particles to turn on when I want but not turn off as I can't access the prefab Shuriken emission.

2.I can't gett it to follow the rock the this script is attached to only be created where it collides. I think I need an update function but I don't know how to fit that in below.

Thanks for any help.

var Debris : GameObject;

function OnCollisionEnter(){

       Instantiate(Debris, transform.position, transform.rotation);

      Emit particles for 3 seconds
       yield WaitForSeconds(3);
       //Then stop
       Debris.enableEmission = false;


}
more ▼

asked Apr 27 '12 at 05:05 PM

StevieMac gravatar image

StevieMac
0 4 4 5

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

2 answers: sort voted first

When "Debris" is your prefab, you might want to save the actual instantiated object which is returned by the "Instantiate" function. You can then access it's components like transform or the particle system.

var actualObject : GameObject = Instantiate(Debris, transform.position, transform.rotation);
// Wait...
actualObject.transform.postion.x += 10; // move it
actualObject.transform.particleEmitter.enabled = false;; // stop the emitter

Something along this, perhaps.

more ▼

answered Apr 27 '12 at 06:19 PM

GerryM gravatar image

GerryM
2.3k 1 3 7

So, the answer is not to change the Instantiate prefab :-)

The Scripting Reference, only 1 click in under the Instantiate heading, also shows this trick. The links are laid out a little funny -- sometimes the best way to navigate the official Unity Docs is with google, but they really do have examples of the "right" way to do almost all of the common stuff.

Apr 27 '12 at 06:38 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

Sorry guys this is really hard for me to get my head around as I'm an artist not a coder. So try to make it as simple as poss. Thanks for the help!

So I have a prefab called Debris & an animated Rock you saying to put the script on the prefab instead of the rock to access the the components of it?

I tried this but got an error.... Assets/Scripts/Prefab Scripts/Rock_Debris.js(1,45): BCE0005: Unknown identifier: 'Rock_Particle_Debris'.

var actualObject : GameObject = Instantiate(Rock_Particle_Debris, transform.position, transform.rotation); // Wait... actualObject.transform.postion.x += 10; // move it actualObject.transform.particleEmitter.enabled = false;; // stop the emitter

more ▼

answered Apr 27 '12 at 10:36 PM

StevieMac gravatar image

StevieMac
0 4 4 5

Maybe you could describe what you want to accomplish game-wise? Should the Rock disappear when the Debris appears? Then the scripts in each object can't refer to each other. You could try a separate (empty) object which contains a kind of manager script. Inside that script you should instantiate (or delete) all the objects you need, keeping a reference. Then you can access the components of these objects easily.

Apr 28 '12 at 01:07 PM GerryM

Hi Gerry I'm not making a game its a demo for showing I can do particle FX in games. So I have a character with Animation's brought in from 3ds Max, that are triggered when the button on the side of the screen is pressed. So I have a button for Earth, you press this & the character smashes the ground a boulder rises & he kicks it away like a special move, this all works fine!

I have my particle FX working I just need a way of triggering more than one at the same time as the rock rises & one of the FX has to be attached to the rock as it rises.

So you press the button on the side, the animation plays & tiggers the FX. The Rock does not disapear it's jus animated off screen to reset to a position under the ground to be used again when the button is pressed again. I have 4 animation's altogether all on the same time line & this Rock/Earth one starts at 78-116. Originally I was hoping just to animate the FX on & off using a timeline but I've read this isn't the way to go about it. So now I'm using the collision route where as the rock collides with the ground, it triggers the FX which I have working but I need to get the debris to fall off the rock so it needs to be attached to it as it rises. I read you make a Prefab then make this appear where the rock does but I need it to follow the rock as it rises this is my problem.

So am I doing this all wrong?

Apr 30 '12 at 02:39 PM StevieMac
(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:

x1671
x1278
x1253
x637
x161

asked: Apr 27 '12 at 05:05 PM

Seen: 866 times

Last Updated: Apr 30 '12 at 02:39 PM