Spawn prefabs with same tags to the scene
im new to coding, so if anyone could help and create this script for me i would apprecheat it sooo much
i wanna spawn Prefabs with the same tag
Comment
Best Answer
Answer by Vicarian · Jan 26, 2017 at 08:35 PM
This script isn't complete by any means, but it should get you started:
using System.Linq;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
[SerializeField] string m_tag = "Untagged";
void Start() {
GameObject[] objects = Resources.FindObjectsOfTypeAll<GameObject>().Where(g => g.tag == m_tag).ToArray();
foreach (GameObject obj in objects)
Instantiate(obj);
}
}
$$anonymous$$ake a script object and add this code to it, then drag the script to an object in the Hierarchy. You can use the tag field that gets displayed to set m_tag
to the value you need. If you want multiple tags, you can change the definition of m_tag to be List<string>
while providing a using directive for System.Collections.Generic
and the Where clause condition to g => m_tag.Contains(g.tag)
.
Your answer
![](https://koobas.hobune.stream/wayback/20220612105658im_/https://answers.unity.com/themes/thub/images/avi.jpg)