So I made a prefab of an enemy in my game. It's a character controller instead of a normal gameobject, so that the movement of the enemy was more advanced. I was wondering if it's possible to randomly generate this object?
Here's my code (which would work if it was actually a gameObject)
using UnityEngine;
using System.Collections;
public class RandomGenerationEnemy : MonoBehaviour {
// Use this for initialization
public GameObject[] enemyArray;
public int amount;
private Vector3 spawnPoint;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
enemyArray = GameObject.FindGameObjectsWithTag("Enemy");
amount = enemyArray.Length;
if (amount != 2)
{
spawnEnemy();
}
}
void spawnEnemy()
{
spawnPoint.x = Random.Range(700, 800);
spawnPoint.y = 21.6f;
spawnPoint.z = Random.Range(500, 600);
Instantiate(enemyArray[UnityEngine.Random.Range(0, enemyArray.Length - 1)], spawnPoint, Quaternion.identity);
CancelInvoke();
}
}
now I've already tried this, but the problem is that it won't work because Enemy isn't tagged. I assume it's because my enemy is a character controller. If this is the case can anyone tell me how I can modify this so it works with character controllers? And if it really is the game object why isn't it working?
↧