Hello,
I’m trying to create a tower defense game that has multiple paths on each level. The example I’m using is Kingdom Rush. In that game, mobs will spawn from different points and follow a set path.
When my units spawn, they get a waypoint assigned and follow that path. My problem is, I can only assign a gameobject from the top array. Unity doesn’t seem to support Arrays within Arrays in the inspector. I know there are workarounds, but they seem convuluted. This works in the tutorial that I was following, but what I’d like to do is have more than one path. My approach is to have an array of paths that contain an array of waypoints. I would then assign the waypoint that is in the spawnpoint array, but I can’t get the value contained in the Spawn points array. If I create a multidimensional array, I can’t access it in the inspector. If I write this: newEnemy.GetComponent<Unit_Base>().waypoints = waves[currentWave].spawnPoints[2];
I get the error saying that spawpoints cannot be used because it is an array, and my assignment is expecting a gameobject.
What is a better approach? How do I access the values of an Array within another array?
Here is my code for refference. Please let me know if anything needs clarifying
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Wave
{
public GameObject[] spawnPoints;
public GameObject[] enemyPrefab;
public GameObject[] waypoints;
// HACK 9-12 DOESNT WORK
public float spawnInterval = 2;
public int maxEnemies = 20;
}
public class SpawnEnemy : MonoBehaviour {
public Wave[] waves; //class object
//9-13 spawn point example
public int timeBetweenWaves = 5;
private float lastSpawnTime;
private int enemiesSpawned = 0;
public GameManager gameManager; //Find the game manager class
// Use this for initialization
void Start ()
{
lastSpawnTime = Time.time;
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
public void Update()
{
int currentWave = gameManager.Wave;
//Check to make sure enemies are left
if (currentWave < waves.Length)
{
// 2
float timeInterval = Time.time - lastSpawnTime;
float spawnInterval = waves[currentWave].spawnInterval;
if (((enemiesSpawned == 0 && timeInterval > timeBetweenWaves) ||
timeInterval > spawnInterval) &&
enemiesSpawned < waves[currentWave].maxEnemies)
{
lastSpawnTime = Time.time;
Vector3 startPos = GameObject.Find("Waypoint0").GetComponent<Transform>().position; //Get the position of the first spawn point in the level
GameObject newEnemy = (GameObject)Instantiate(waves[currentWave].enemyPrefab[Random.Range(0,waves[currentWave].enemyPrefab.Length)], startPos, Quaternion.identity); //Spawn a random enemy
newEnemy.GetComponent<Unit_Base>().waypoints = waves[currentWave].spawnPoints[2]; // waypoints; //Set the waypoint of the enemy
//
//need to make two choices
enemiesSpawned++; //add to enemies spawned
}
// Check to see if the last enemeis have been spawned in current wave
//If they have,
if (enemiesSpawned == waves[currentWave].maxEnemies &&
GameObject.FindGameObjectWithTag("Enemy") == null)
{
gameManager.Wave++; //increase to wave
enemiesSpawned = 0;
lastSpawnTime = Time.time;
}
} else{
//If no more enemies, and player still has lives call the win function load next level
gameManager.gameOver = true;
}
}
}