Jake33011
Jake33011

Reputation: 11

Unity how to turn all lights off in prefabs

Hey guys I'm currently creating a little mobile game following a series on youtube and everything goes very fine, the game is working great and everything but I have one problem.

So basically what the game does is to choose randomly a model from my prefabs, place it at a certain position and then does the same over and over again until you loose.

But I just saw that when I created this models (on blender) I created some lighting system on all of them. So now, when they are instantiated there is too much light in the scene. I know I could simply reimport them without the lights but the problem is that I created a lot of prefabs using these models from blender, so I would have to start almost everything again.

I was wondering if there was some script or a way to do that so when the prefabs are instantiated, all the lights attached to them are disabled.

I also tried to go in the folder of the imported models or the first prefabs made and untick all the lights (Hemi) but when the GameObject is instantiated, the lights are still here. I think this is because I should make the change in the scene and then save it back in the folder but again I would have to do it all over again.

Here is the way the prefab is made :

Imported model -> First prefab -> Piece (2nd prefab) -> Segment (last prefab)

And it is the segment that is instanciated (so with all the others in it). Thanks for your help :)

Upvotes: 0

Views: 1675

Answers (3)

user7436930
user7436930

Reputation:

You can set maximum amount of spawned lights and disable new instantce's Light if maximum is reached. Create const integer MAX_LIGHTS_COUNT. Set static integer "lightsCount" counting spawned objects with lights, increase it on spawn and decrease on "death" event. When you are spawning prefab increase counter, check if lightsCount>=MAX_LIGHTS_COUNT, then disable Light components.

const int MAX_LIGHTS_COUNT = 20;
static int lightsCount = 0;
...
void someSpawningMethod (...) {
    GameObject instance = Instantiate (prefab) as GameObject;
    if (++lightsCount >= MAX_LIGHTS_COUNT)
        foreach (Light l in instance.GetComponentsInChildren<Light>())
            l.enabled = false;
 }

Also you can run over all your prefabs on game start and disable lights.

GameObject[] prefabs;
void Start () {
    foreach (GameObject go in prefabs) 
        foreach (Light l in go.GetComponentsInChildren<Light>())
            Destroy (l);

}

Upvotes: 0

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

First add a tag to the lights in your scene you would like to keep on. For example "scene_lights"

Then access all the GameObjects of type Light in your scene and reduce to 0 their intensity, but skip those of tag "scene_lights".

void Start()
{
    LightsGO = FindObjectOfType(typeof(Light)) as GameObject[];
    foreach(GameObject thisLight in LightsGO)
    {
        if(thisLight.tag != "scene_lights")
            thisLight.light.intensity = 0;
    }
}

Upvotes: 1

Alvin
Alvin

Reputation: 226

If it's a prefab, you can just edit the prefab. Setactive false or disabled the light in that prefab and try to instantiate it.

Upvotes: 0

Related Questions