Answers for "instantiate prefab at every gameobject with tag"

C#
1

instantiate prefab at every gameobject with tag

[SerializeField] private GameObject spawnerPrefab;
    // Start is called before the first frame update
    //C# script because grepper won't let me select c#, big sad
    void Start()
    {
    	//you also need UnityEngine.SceneManagement
    	/*you do need a gameobject sceneloader for this with a fuction that returs the current scene index.
        eg public int GetCurrentScene()
    	{
        	return SceneManager.GetActiveScene().buildIndex;
    	} */ // or you can just put it all on this script...
        SceneLoader sceneLoader = GameObject.FindGameObjectWithTag("Scene Loader").GetComponent<SceneLoader>();
        int currentSceneIndex = sceneLoader.GetCurrentScene();
        if (currentSceneIndex != 1)
        {
            return;
        }
        else
        {
            var enemyPos = GameObject.FindGameObjectsWithTag("Enemy");
            for (int i = 0; i < enemyPos.Length; i++)
            {
                Instantiate(spawnerPrefab, enemyPos[i].transform.position, enemyPos[i].transform.rotation);
            }
        }
    }
Posted by: Guest on January-27-2021

Code answers related to "instantiate prefab at every gameobject with tag"

C# Answers by Framework

Browse Popular Code Answers by Language