Utterly confused

Reposting this from Udemy. :blush:

Up until now, I’ve been able to preempt what Rick wants me to do in the coding challenges. It’s gratifying that I have enough of an understanding that I complete the challenge successfully before Rick demonstrates his solution. :blush:

Well, that ground to a halt in the Public Methods & Return Types video. :expressionless:

I understand the concept of being able to access code from one class in another by making the code public.

That’s basically where my comprehension stops. Even Rick’s “Calling A Method” slide makes no sense to me, because I can’t visualise the context. This is unlike earlier videos, where his illustrations made sense because we’d already been poking around the relevant areas of Unity or c#, so we could determine context.

Rick calls a challenge to Create Public Method, and I have zero idea what to do:

“Create a public method called GetStateStory, that’ll be the name of our method”

I’ve seen public class written before, but what is a method in this context? Have I forgotten some critical detail? This is my third replay of this module.

“Use the string return type”

I’ve been writing strings comfortably now, but I have only a very vague understanding of his instruction here. We’re going to return a string type, because the method we’re returning is written text? I’m finding it really difficult to visualise what he’s asking here, and what I’m supposed to do.

“We’re going to contain one statement, and that statement will be just returning-- it will use the return keyword to return our storyText variable”

I understand that we created the storyText variable as part of our StartingState scriptable object. This is where my understanding ends.

When I did this module before, I just soldiered on, thinking I’d understand later, but it seemed like my lack of understanding here was setting me up for failure. I continued to be confused.

I know Rick mentioned that this section of the course would need more concentration for beginners to wrap their heads around, but is there a chance Rick might be able to take another crack at teaching this section?

I’m going to keep reading the other comments and questions students have left, in hope of understanding what’s going on, but so far I haven’t had much luck.

Yeah the same thing happened for me. All was well until this one. I ended up getting to frustrated and walked away for a few hours. Came back with a clear head and just rewatched it all again. Started taking notes, and enough started to click for me to get through it.

1 Like

Hi Belinda,

A method is effectively a behaviour that the class can perform. Sometimes these may be private, so only the class itself can access (call) them, and in other situations you may have public methods. Public methods will be visible to other classes within the same library/application, in this case your game.

As an example;

using UnityEngine;

public class Player : MonoBehaviour
{
    public void Jump()
    {
        // do jump code here
    }
}

In the above, we have a class named Player, it has a public method name Jump, this method could be called by other code both within the Player class, and also by other classes in the same application.

To use this method from another class you would create an instance of the Player class, and then you would be able to access its public member variables/methods. As an example;

using UnityEngine;

public class Ghost : MonoBehaviour
{
    private void Start()
    {
        Player player = FindObjectOfType<Player>();

        player.Jump();
    }
}

In the above Ghost class we have a Start method, there’s a line of code there which is probably not familiar to you yet, sorry, but it was the only way to give a bit of a working example. The FindObjectOfType method you will see throughout the course, it is used to find a GameObject in the scene of a specific type. So, out above example assumes that it is goes to find a GameObject in the scene with the Player script attached to it. It then stores the reference to that object in the variable player, which is of type Player.

The next line, which was really the bit I wanted to demonstrate uses the instance of the player class, our reference to the GameObject it found, and calls the Jump method.

The Jump method above also has a return type of void, in this case that means it effectively isn’t returning anything, but you can also have other types as return types, for example;

using UnityEngine;

public class Voice : MonoBehaviour
{
    private void Start()
    {
        Debug.Log(Greeting());
    }

    public string Greeting()
    {
        return "Hello Belinda";
    }
}

In the above example, the method Greeting has a return type of string. Within the method we return a string, “Hello Belinda”. This is then output via the Debug.Log statement within the Start method.

I hope the helps, if anything is unclear please do let me know :slight_smile:

1 Like

Thanks so much for the detailed reply, Rob! I appreciate you taking the time (especially this time of the year!) to point me in the right direction.

You’ve managed to cover a chunk of what was confusing me.

I also took James’ advice and went back through the course to take lots of notes! I noticed that Rick mentioned some things in passing that my brain dismissed as “something that will be explained later”, only to find it actually wasn’t. Notes are helpful. :laughing:

I created a new forum post that shares my current understanding of the lesson: Okay, I think I've figured this out! Correct me if I'm wrong! :)
I’d HUGELY appreciate it if folk could point out where I might have things a bit muddled up. :blush::blush::blush:

Thanks again for the help, this community is exactly why I chose to do this course over others! :blush:

1 Like

You’re very welcome, I’m glad it was of use.

As requested, I have made a couple of comments on your other topic. Very nice write up by the way.

1 Like

Privacy & Terms