x


activating a prefab in scene

Is it possible to have a prefab in my scene but inactive (unchecked in the inspector) then on a trigger event activate it?

what I am trying to do is have a forcefield activate, i've tried instantiating on trigger (and parenting to an empty gameobject) but this fails as for some reason keeps appearing in different locations.

more ▼

asked Jun 16 '12 at 10:19 PM

carter.carl30 gravatar image

carter.carl30
46 11 18 21

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

1 answer: sort voted first

Yes you can do that. When you want to activate it - call forcefieldGameObject.SetActiveRecursively(true) then it and any children will activate. Note that you need a reference to the game object, you cannot find it with GameObject.Find.

Just a note on your problem with instantiating - you should probably make sure that you set the localPosition of the force field to 0,0,0 after you set its parent if you use that approach.

more ▼

answered Jun 16 '12 at 10:27 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 99

by reference do you mean do a var forcefield : GameObject; ?

Jun 16 '12 at 10:59 PM carter.carl30

Make sure the object you referenced in the inspector as "forcefield_powerup" is the instance of your prefab that you manually dragged into the scene, not the actual prefab in the "Project" view.

Jun 16 '12 at 11:45 PM Wolfram

Select the prefab in the hierarchy view and drag it onto the forcefield_powerup. That will work if the prefab is instantiated in the scene.

Jun 17 '12 at 09:13 PM whydoidoit

Which object do you have selected, meaning, which object is shown in the inspector? The actual script? Or the object your script is attached to?

Jun 17 '12 at 10:26 PM Wolfram

You cannot assign scene references to a prefab. We thought the object you were talking about was also part of your scene, not part of a prefab itself. If it's not possible for you to use a scene instance for that prefab, too, you'll have to use a different method to assign the reference. For example, create a GameObject var in the script that actually instances "save_line_powerup", where you can drag the forcefield_powerup object (unless that instancing script is also merely a prefab...), and then assign it via script, directly after the line that instances "save_line_powerup".

Jun 17 '12 at 10:57 PM Wolfram
(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:

x1253
x133
x113

asked: Jun 16 '12 at 10:19 PM

Seen: 444 times

Last Updated: Jun 19 '12 at 05:27 PM