- Home /
Why Does this Work on a Hierarchy Object but NOT a Prefab?
Why does this script only work on objects/child objects I drag and drop from the hierarchy and not on ones I drag from my prefabs?
#pragma strict
var torch : GameObject;
function Start ()
{
torch.SetActive(false);
}
function OnTriggerEnter()
{
torch.SetActive(true);
}
function OnTriggerExit()
{
torch.SetActive(false);
}
(the script is used to make it so the lights in a room/area only come on when the player is in that area)
Thanks
because prefab does not exist in the scene unless you instantiate it in the scene with the Instantiate() method. look it up in the unity references. :))
All the items are already in the scene. All I basically want to do is turn the flames on (make them active) when the player enters that game area and turn off once the player leaves the area
if you first drop the prefab in the scene and then drop the same prefab on the 'torch' slot that won't work. you need to select the object in the scene and drop it on the 'torch'.
Which works fine for that one item. But I have 6 torches/flames in the room. Only the specific one I drag over is affected, not all of them?
ok, then this is not so professional but easily understandable way: create 6 variables called torch1 torch2 torch3... and implement all the sentences that you have for your torch for each of the torches.
if you know arrays and for loops you can achieve the same effect by introducing an array of GameObjects and looping each enabled/disabled commands through all of the using the for loop, google arrays and google 'for loops' for more info
Answer by Paulocmfo · Dec 17, 2014 at 07:31 PM
Through code you only can put a prefab in scene through Instantiate() method. See script reference for details.
Your answer
Follow this Question
Related Questions
How to destroy all gameobjects active in hierarchy? 1 Answer
Check if game object is active in hierarchy from a prefab 2 Answers
Adding extra bones to an already imported and prefab'ed character. 1 Answer
GameObject is missing a prefab, assign prefab to gameobject 1 Answer
Why cannot I drag and drop a gameObject from Hierarchy to a prefab's slot in the Inspector? 3 Answers