Instantiate isnt working for spawner class
Forgive me, I've found a few questions similar to this but all of the fixes have been rather semantic nuances. Im trying to spawn my 'Enemy' object, which is just a cube at the moment at 1 of 3 random spawn points in my scene.
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject Enemy;
GameObject[] objs;
int top;
void Start()
{
objs = GameObject.FindGameObjectsWithTag("Spawn_Point");
top = objs.Length;
InvokeRepeating("CreateObject", 0.01f, 5.0f);
}
void CreateObject()
{
Instantiate(Enemy, objs[Random.Range(0,top)].GetComponent<Transform>().position, Quaternion.identity);
}
void Update () {
Instantiate(Enemy, new Vector3(1, 2, 1), Quaternion.identity);
}
}
Neither the CreateObject() or Update() functions create a new object. Even the basic code given in the instantiate documentation fails.
using UnityEngine;
using System.Collections;
public class TestInstantiate : MonoBehaviour {
public GameObject prefab;
void Start()
{
for (int i = 0; i < 10; i++)
Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
}
}
Heres a picture of my unity scene with the settings on my Enemy prefab. [1]: http://i.imgur.com/BoW8q4v.png
Answer by dean904 · Mar 17, 2016 at 04:48 AM
Well, I feel stupid. I needed to apply the scripts to an object in my scene. Placing the scripts on the ground object activated the scripts and everything began working.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I spawn a certain amount of objects randomly between 2 positions? 0 Answers
Crash when instantiating two Prefabs with the same tag 0 Answers
Instantiate PreFab on trigger location 0 Answers
Can I spawn with an array in order? 2 Answers
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers