Question by
Zeroandx · Jan 31, 2016 at 11:16 PM ·
prefabinvokerepeatinginstantiating
Prefabs not instantiating when first run
So I have a script that's supposed to instantiate a bunch of objects randomly chosen from an array depending on a value taken from another array, all using InvokeRepeating. It worked when I was testing it out but when I made a build of the game the objects weren't being instantiated. I also noticed that when I closed unity and opened it again I got the same problem when I ran it the first time, but when I stopped it and ran it again it worked fine. I'm not getting any errors or anything so I'm not sure whats going on. Here's the code that's supposed to instantiate the objects.
void Start() {
InitiateSpawn();
}
// Update is called once per frame
void Update () {
Debug.Log("Time is " + Time.realtimeSinceStartup);
}
void startTheMusic() {
theMusic.enabled = true;
}
void InitiateSpawn()
{
InvokeRepeating("arrowSpawning", 0, 1 / 1.916667f);
}
void arrowSpawning() {
Debug.Log(arrowCounter);
//spawns arrows based on the sample value
if (sampleSelection[arrowCounter] < -0.25 && sampleSelection[arrowCounter] > -0.5) {
Instantiate(arrows[1], new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
}
if (sampleSelection[arrowCounter] < -0 && sampleSelection[arrowCounter] > -0.25)
{
Instantiate(arrows[0], new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
}
if (sampleSelection[arrowCounter] < 0.25 && sampleSelection[arrowCounter] > 0)
{
Instantiate(arrows[3], new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
}
if (sampleSelection[arrowCounter] < 0.5 && sampleSelection[arrowCounter] > 0.25)
{
Instantiate(arrows[2], new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
}
arrowCounter++;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612062511im_/https://answers.unity.com/themes/thub/images/avi.jpg)