Hello,
I’m having some trouble with the foreach section. My code looks identical to yours, and I’ve checked it several times, except that I called “EnemyPathing” “EnemyWaypoints” instead. However, I get a run-time error when I try to play.
When I try to run the game to see if the 2nd path created works, I get an error on the line:
waypoints = waveConfig.GetWaypoints();
I noticed that when I mouse over my config params where I serialized this field it shows the following:
Is this normal? It seems to not be able to get my waypoints
For more info, see the rest of my code below:
WAVECONFIG.CS
[CreateAssetMenu(menuName = "Enemy Wave Config")] //This makes "Enemy Wave Config" show in the menu when you R-Click on the assets area, and when you choose it it creates a "wave" file
public class WaveConfig : ScriptableObject {
// config params
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpawns = 0.5f;
[SerializeField] float spawnRandomFactor = 0.3f;
[SerializeField] int numberOfEnemies;
[SerializeField] float moveSpeed = 2f;
public GameObject GetEnemyPrefab() {return enemyPrefab;}
public List<Transform> GetWaypoints()
{
var waveWaypoints = new List<Transform>();
foreach (Transform child in pathPrefab.transform)
{
waveWaypoints.Add(child);
}
return waveWaypoints;
}
public float GettimeBetweenSpawns() { return timeBetweenSpawns; }
public float GetspawnRandomFactor() { return spawnRandomFactor; }
public int GetnumberOfEnemies() { return numberOfEnemies; }
public float GetMoveSpeed() { return moveSpeed; }
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
ENEMYWAYPOINT.CS
[SerializeField] WaveConfig waveConfig;
List<Transform> waypoints;
[SerializeField] float pathMoveSpeed = 3f;
[SerializeField] int waypointIndex = 0;
// Use this for initialization
void Start () {
waypoints = waveConfig.GetWaypoints();
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update() {
MovePath();
}
private void MovePath()
{
if (waypointIndex <= waypoints.Count - 1)
{
var targetPosition = waypoints[waypointIndex].transform.position;
var movementSpeedThisFrame = pathMoveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementSpeedThisFrame);
if (transform.position == waypoints[waypointIndex].transform.position)
{
waypointIndex++;
}
}
else
Destroy(gameObject);
;
}
}