Question about SetWaveConfig

After a bit of effort, I now understand why we used the SetWaveConfig() method inside the EnemyPathing class, but what I don’t understand is how it works. If it’s written after the Start() and Update() methods, how does it find and change the variable after the variable has already been called? For some reason, I thought the start method was usually the first method called, and it was only called once. I feel like I’m wrong about something fundamental here, and I’m hoping that someone with a little more knowledge than I have can show me what I’m missing.

I’ve attached the script for your reference. (Please pardon all the comments. Like I said, I only understand this new method after a bit of effort.)

Hi,

First of all, it’s great to see that you are trying to understand what’s going on in Rick’s code. Admittedly, it’s a bit more complex than what he normally does because we pass on data between instances.

Variables never get called. Methods get called. You can access variables, though. Furthermore, we never pass on variables. It looks as if we did but in reality, we pass on a value.

Unfortunately, reading text on screenshots is a bit exhausting for me, especially when it comes to longer texts. If you shared your screenshot because you want us to read your comments, it would be great if you could share your code as formatted text. This way, we could copy and paste parts of your code and/or comments if necessary. :slight_smile:


See also:

Thanks Nina. I really appreciate your help. I’ll make sure I’m posting code correctly from here on out. Here is the code from the lesson.

public class EnemyPathing : MonoBehaviour
{

    WaveConfig waveConfig;
    List<Transform> wayPoints;
    int waypointIndex = 0;
    
    // Start is called before the first frame update
    void Start()
    {
        //when wayPoints is accessed, it fills its list with transform data from the GetWayPoints method in the waveConfig Scriptable Object. 
        wayPoints = waveConfig.GetWaypoints();
        transform.position = wayPoints[waypointIndex].transform.position; //enemy starts at the first waypoint
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    //This method is meant to set this class's WaveConfig scriptable object to the same one being used by the Spawner. 
    public void SetWaveConfig(WaveConfig waveConfig)
    {
        //"this" refers to the "waveConfig" belonging to the class EnemyPathing instead of the local variable referred to in the the method. 
        this.waveConfig = waveConfig;
    }

If I’m understanding it, we’re accessing a value from another script and setting our waveConfig variable equal to it for use in this instance. Since waveConfig is undefined before the SetWaveConfig method, does Unity wait to execute the Start method until waveConfig is defined?

Thank you again for your help.

Thank you. That’s much better! :slight_smile:

Since waveConfig is undefined before the SetWaveConfig method, does Unity wait to execute the Start method until waveConfig is defined?

By default, no program ever waits for anything. It gets executed and that’s it. You have to write asynchronous code to wait for data to be returned but we do not cover that in this course.

So the question is: In what order do the methods get executed? Add Debug.Logs with Time.frameCount and meaningful messages to your code. Run your game and check your console. I’m sure when you read the messages, you’ll immediately know what’s going on here.


transform.position = wayPoints[waypointIndex].transform.position; //enemy starts at the first waypoint

To be more precise: The enemy starts at the waypoint at index waypointIndex given the index is within the range of the array and if the element at that position is not null. If the enemy starts at the first waypoint, the condition is true only if the waypointsIndex points to the correct index to which the first waypoint was assigned.

//This method is meant to set this class’s WaveConfig scriptable object to the same one being used by the Spawner.

To this instance’s WaceConfig object. In C#, there is a big difference between classes and instances. Classes are basically blueprints. Instances are the actual objects.

//“this” refers to the “waveConfig” belonging to the class EnemyPathing

To this instance of the EnemyPathing class. If it referred to the class, all EnemyPathing instances would get affected if you change the this.waveConfig variable. The non-static variables at the top of your class are called instance members.

2 Likes

So the question is: In what order do the methods get executed? Add Debug.Logs with Time.frameCount and meaningful messages to your code. Run your game and check your console. I’m sure when you read the messages, you’ll immediately know what’s going on here.

This was incredibly helpful advice and it cleared up a lot of my confusion.

Thank you for the clarification on class v instance too. The code makes a lot more sense now that I see the distinction.

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

Privacy & Terms