Issue with getWaypoints()

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:

image

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);
            ;
        }
}

Hi Tamarack,

Perhaps you could share the error that you receive also?

This is the error I am seeing

image

This is my enemy prefab:

This is my wave:
image

This is my wave:

Thanks.

I’m guessing the top of your EnemyWaypoint.cs script wasn’t copy/pasted into your post (using directives / class defintion) - but this is line 15 correct?

waypoints = waveConfig.GetWaypoints();

Assuming so, you are trying to access the method GetWaypoints from waveConfig, but waveConfig is null.

If you look above in your code you will see that you have set this to be serializable so that it is exposed within the Inspector. Did you perhaps forget to drag in the relevant object to this field?


Updated Mon Oct 29 2018 23:44

Ah, you added the screenshots just as I posted. It looks ok from the screenshots.

Could you zip up your project files and share them, I will happily take a quick look for you.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

I will do that, no problem.

Is it normal for WaveConfig waveConfig to point to WaveConfig EnemyWaypoint.waveConfig if I mouse over it? I thought it might be looking inside the EnemyWaypoint Script instead of the WaveConfig script, even though I pointed it there.

Probably be easier if I can see it all in context, pop the URL up and I’ll take a look.

This should be it:

https://drive.google.com/file/d/1GEsoZOF6QDXW4JxEHSmwZtLJ58YGaJDj/view?usp=gmail

Can you set it to public please :slight_smile:

https://drive.google.com/open?id=19DDgnQ40yciFKzBUpCtXWwf8l-GU9Sts

Please let me know if that works. I don’t use Google Drive very often, so I’m not used to navigating the HMI.

I’m seeing an empty folder called Laser Defender.

If I download it I just get a corrupted zip file that won’t open.

Are the files inside that folder? If so, do they have public permissions also?

S
orry, try again please

1 Like

No worries, this looks better, I can see a zip file, 27mb… downloading now…

Give me a few minutes as I will need to open the project at this end and the first time opening normally takes a little longer, especially if we are using different versions of Unity.

No Problem. Thanks for looking into this for me.

I should mention that I took on a “challenge” and wrote the WaveConfig.cs on my own at first under a different name, and then changed the name to align with your lesson (to avoid my own confusion). I don’t know if that is part of the issue or not.

1 Like

If you select the Enemies GameObject in the Hierarchy, you’ll note that you’ve added a EnemyWaypoint.cs script component to it, because the properties for this one haven’t been set, you are getting the error message described.

Remove the EnemyWaypoint.cs script component from this GameObject and run the game, all should be well.

The issue is unrelated to the challenge, so well done for completing that yourself by the way :slight_smile:

It worked! Thank you very much!

1 Like

You’re more than welcome Tamarack, I am glad you can move forward again :slight_smile:


Updated Tue Oct 30 2018 00:26

Oh, and I didn’t answer your other question, sorry, what you see here when you hover;

image

…is defintion of what you are hovering over, so firstly, we are being told it is a field (a member variable).

Next, you have the type which is WaveConfig - the class that you have defined.

The last part, which I think is what has caused the confusion for you, is its name in the format of class.field, e.g. this class “EnemyWaypoint” dot “waveConfig”

Hope this makes sense.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms