- Home /
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.
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;
}
Answer by GerryM · Apr 27, 2012 at 06:19 PM
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.
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.
Answer by StevieMac · Apr 27, 2012 at 10:36 PM
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
$$anonymous$$aybe 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.
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 $$anonymous$$ax, 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?