Hello, I have a problem with the code from the laser defender section of the unity 2d course
when calling the GetWayPoints() method from a scriptable object from the following code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "WaveConfig")]
public class WaveConfig : ScriptableObject
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float spawnTimer;
[SerializeField] float randomness;
[SerializeField] int enemyNumber;
[SerializeField] float moveSpeed;
private void Start()
{
}
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;
}
I call it in another script in the start method.
public class EnemyPathing : MonoBehaviour
{
WaveConfig waveConfig;
[SerializeField] List<Transform> wayPoints;
[SerializeField] float moveSpeed;
[SerializeField] int wayPointIndex = 0;
private void Start()
{
transform.position = wayPoints[wayPointIndex].transform.position;
wayPoints = waveConfig.GetWayPoints();
}
private void Update()
{
Move();
}
private void Move()
{
if (wayPointIndex <= wayPoints.Count - 1)
{
var targetPos = wayPoints[wayPointIndex].transform.position;
var moveFrameBalance = moveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, targetPos, moveFrameBalance);
if (transform.position == wayPoints[wayPointIndex].transform.position)
{
wayPointIndex++;
}
}
else
{
Destroy(gameObject);
}
}
}
But it gives me the error “ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index”
The method is supposed to return a list.
So when i tried to print the array using Debug.Log(waveConfig.GetWayPoints());
without calling the function anywhere else, it still gives me the error and does not print anything.
I think it is a problem within the method itself.
Please help !