0
votes

I created a script that instantiate a variety of different prefabs (also can instantiate more than one of the same prefab) that becomes a child of a parent (gameobject) I called slot. Once spawn each prefab has a set transformation position to a certain area, so say if I wanted my script to spawn in 3 of the same prefab (say prefab1) in to my scene, all three prefab1 would have the same transform position (it would be layered on top of each other) and also would be a child to the slot. But say if I spawn in 4 of prefab2 then prefab2 would have a different start position compared to prefab1, but is set under the same parent as prefab1...hope I'm making sense! What I want to do: is there a way to setactive false all instantiated prefabs but leave one of all the different prefabs that was spawn, for instance:

enter image description here

I want to setactive false all of the prefab1 and prefab2 within this black box I drew above the image and leave the one prefab1 & prefab 2 setactive to true, if the prefab1/prefab2 is deleted by collision then I would like the next prefab1/prefab2 to be setactive to true and so on. Again I hope I'm making sense... this is my code for spawning in my prefabs:

 void Update () {
   for (int x = 0; x < slotController; x++) {
            var item = Instantiate (ItemPrefab) as GameObject;
            itemPrefab.transform.position = transform.position;
            itemPrefab.transform.SetParent (slot.transform);
            itemPrefab.SetActive (true);
            if (slot.childCount < 0) {
                slotHolder.SetActive (false);
            }
        }
    }
1

1 Answers

1
votes

You can use collections and LINQ to do things like this. Here is an example. The "Thing" class would be your prefabs.

You basically have a List collection for each type of prefab you have, and a dictionary that stores all those lists, and has a key to access them with.

(By the way, the Update method means your code will happen every frame so that may not be a good place for it)

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Dictionary<int, List<Thing>> prefabs = new Dictionary<int, List<Thing>>();

        // add a few items to the lists
        prefabs.Add(1, new List<Thing>(){ new Thing("Thing1"), new Thing("Thing1") });
        prefabs.Add(2, new List<Thing>(){ new Thing("Thing2"), new Thing("Thing2"), new Thing("Thing2") });

        var keys = prefabs.Keys;
        // set everything inactive except the first one
        foreach(int i in keys){
            prefabs[i].Skip(1).ToList().ForEach(x => x.active = false);
        }

        // print out the active state
        foreach(int i in keys){
            foreach(Thing t in prefabs[i]){
                Console.WriteLine("prefab " + i + "  active = " + t.active);
            }
        }
    }

    class Thing {
        public bool active = true;
        public String name = "";

        public Thing(String name){
        this.name = name;
        }
    }
}

Result:

prefab 1  active = True
prefab 1  active = False
prefab 2  active = True
prefab 2  active = False
prefab 2  active = False