- Home /
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.
Answer by whydoidoit · Jun 16, 2012 at 10:27 PM
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.
by reference do you mean do a var forcefield : GameObject; ?
$$anonymous$$ake 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.
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.
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?
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".