- Home /
How to instantiate an object on multiple positions?
So I have built a cheat code system, where you enter a code on the screen and then when you load up the leaves the cheats would kick in. Some are more easter eggs than cheats, sorta like in the lego games, where the jedis are using a broom instead of a lightsabers, mine would spawn masks onto all the characters on the screen (see below image). Ive put an empty game object as a child of each of the characters, where there heads would be.
The problem is either the code im using to put all of the empty child objects into an array isnt doing its job or the instantiate code im using isnt working. I had a version that worked, but that was before the cheat system was implemented which was a public array so that I can drag each object into the array but since the cheat code system is in a different scene, I had to make it so that the object that contained the cheat code system didn't delete when loading other levels, which means when each level is loaded the code would have to find each object itself. THe code works in a sense but doesnt spawn the masks, either because the array is empty or the instantiate code is wrong, but when I test the code the debug logs are activated so it is going through each step.
This code is used to activate the cheat when the actual level is loaded.
if (Application.loadedLevel == 1)
{
maskSpawnPoints = GameObject.FindGameObjectsWithTag("maskSpawnPoints");
if (spawningMasks == true)
{
if (koalaCheatActivated == true)
{
Debug.Log("Spawning Masks");
StartCoroutine(SpawnKoalaMasks());
spawningMasks = false;
}
/*if (catCheatActivated == true)
{
StartCoroutine(SpawnCatMasks());
}*/
}
}
And this is the Initiator code
IEnumerator SpawnKoalaMasks()
{
Debug.Log ("Spawning Koala Masks");
yield return new WaitForSeconds(1.0f);
for(int i = 0; i < maskSpawnPoints.Length; i++)
{
Instantiate(koalaMask, maskSpawnPoints[i].transform.position, maskSpawnPoints[i].transform.rotation);
}
}
That should be everything that I could think you might need to help find a solution and thanks in advance.
Your answer
Follow this Question
Related Questions
Instanciate prefab responsively 1 Answer
Object created through Instantiate does not update its position at the first frame 1 Answer
Animation doesn't affect the new instantiated object. 0 Answers
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Instantiate an instance? 1 Answer