About 'Public Methods & Return Types'!

In this video (objectives)…

  1. Public versus private access modifier.
  2. What are return types for methods.
  3. Writing a public method and accessing it from another class.

After watching (learning outcomes)…

Create a public method and access it from another class.

(Unique Video Reference: 8_TX_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Hei @Rick_Davidson thank you for the series! I have a question. So i have some background with OOP and i understand you basically instantiated an object of type State and everything; However there are two things that i am confused about. 1) when you instantiate an object, for example ‘’'state", you did : State StartingState;
however shouldn’t you do it like so? State StartingState = new State();
2) Also i am confused about the part when you drag and drop the scriptable object onto the object “StartingState” in the unity inspector; i am more of a coder and would like to know what actually happens “under the hood” of unity when you do that, maybe you can give me an example of how you would do that without actually working in unity but actually writing code which will do the exact same thing so i can understand it better.Also one last thing, is the scriptable object actually an instance of an object from the State class? An answer from either the community or you Rick would be fantastic! Thank You!

Hi Luca,

  1. When we are writing State startingState;we are not instantiating an object, we are declaring a variable. The variable is called startingState and the type is State which matches the class we have created (our other script - State.cs). The reason we create this variable is so we can assign a particular Scriptable Object as the first one to start with (ie. our starting state).

  2. The process of dragging the .asset file (our scriptable object in this case) is the same as if we clicked on the selector and assigned it (the little circle next to the field). In pseudo terms (not the actual code) it is the equivalent of saying State startingState = TheNameOfTheFile.asset. We are storing the name of the file into the startingState variable and then whenever we use the startingState variable later we know which file specific asset file has been assigned to it. In Unity we use the convenience of the editor to save us from having to hard-wire every relationship in code.

Hope that helps.

2 Likes

Thank you Rick for the clarification. So what i understand is that that our variable of type State, or any other class type variable for that matter, can store objects of that type; thats why you can use the dot operator on the variable of type State, because your variable actually holds an object. Is that right ?

(QUESTION!) I am having difficulty understanding what I’ve done wrong… So nine minutes into the lecture you play the script for the first time and it all works out great. I however am getting errors. specifically here void Start ()
{
state = startingState;
textComponent = state.GetStateStory();

}

Under state.GetStateStory(); im getting a red line stating I "Cannot implicitly convert type ‘string’ to ‘UnityEngine.UI.Text’ "

What does this mean?

as far as I can see my script is identical to yours, but I’m sure I’m missing something.

I believe you’re missing .text… it should read:

textComponent.text = state.GetStateStory();

ahh, well. I’m good at life.

{Solved}
I’m currently getting this error in unity “Assets/AdventureGame.cs(17,36): error CS1061: Type State' does not contain a definition forGetStateStory’ and no extension method GetStateStory' of typeState’ could be found. Are you missing an assembly reference?” , this is after we have finished our code in VS followed by saving and running unity .

  • Thanks Again
    P.S really enjoying the course so far :slight_smile:
    Update : For some odd reason the save was not processing properly , got it to work this morning thanks for the quick reply Rick !

If you can share your scripts with us, we might be able to help track down the trouble for you.

I have a quick question about declaring the state variable. Is it really required since startingState is already there? Can we replace textComponent.text = state.GetStateStory(); with textComponent.text = startingState.GetStateStory(); (this is in the AdventureGame.cs). Does it have something to do with [SerializeField] or just bad coding practice?

Mostly its for code clarity. The only thing we need control over in the inspector is the first state, because we need to explicitly point the code to which state we want to start with (hence called startingState). As the game works through its logic and game progresses, we need a variable to hold which state we are in. It wouldn’t make sense to call that startingState because it no longer is the startingState. So startingState helps us know where we are starting and state helps us to know what state we are currently in.

1 Like

I’m getting an error when running the script although I’m sure I have the script the same as yours. Have I missed a step?

image

image

I think you probably forgot to attach the scriptable object “StartingState” to the “startingState” property of the “Game” game object (see video of lecure at around 6:44 minutes).

If you don’t attach the object, startingState is null, and then the program crashes when it tries to access state.GetStateStory()

3 Likes

Is anyone else finding this a bit hard going?

I come from a web design background and I have a pretty thorough grasp of HTML/CSS. Now, I was pretty pleased with the fact that I was managing (albeit sometimes with the skin of my teeth) to keep up with the C#, however, about halfway through this lesson, it suddenly seemed as if I’d accidentally increased the video speed. State this, state that, blah blah state, state aaaaaaaagh. I’ve rewatched the video a few times, and I’ve even started over from the beginning of this sections, but I really can’t grasp what’s happening. I just wondered if anyone else was having trouble, or if I’m just unbelievably stupid (I know it’s a possibility).

7 Likes

I absolutely had trouble this video. Same thing, where I thought I had grasped everything well until this point and then all of a sudden I was seeing and hearing things that I didn’t previously know. This was the first video that I think could’ve had a slower explanation of things.

1 Like

[Question] At about 6:38 in the video, where “Starting State” appears in the inspector under “Game”, it doesn’t appear for me. I made sure my script was the exact same as yours, however it never appeared in unity. I’ll attach some screenshots.Game%20Code


I appreciate the help.

1 Like

Thanks so much. This was precisely the problem. Can’t believe I missed this step

Hey @Simon66

Don’t stress if this doesn’t make sense. Keep pushing on with the course and come back and visit the tricky bits after you’ve completed the later sections. A lot of these concepts only click once you’ve implemented them 5 times in 5 different contexts.

1 Like

Thanks Rick. I’ve spent the last week gently banging my head on the desk, and now I feel refreshed, focused and ready to jump back in :wink:

1 Like

Hey Rick, Thank you…the lectures are going super awesome!!!

I come from a completely non-coding background and wanted to know when and for what purpose shall we use different type of brackets - { } , ( ) , [ ].
This is to understand and make my concepts strong so that i know when and where should I use these.

Thanks,
Harshal

Privacy & Terms