About 'Public Methods & Return Types'!

Hi Harshal,

I don’t think there are any hard and fast rules for the types of brackets, but some typical uses of them.

[ ] usually associated with collections of data such as arrays or lists
{ } usually used to denote a code block, that is all of the code which is grouped together at the same level
( ) quite often used to add details / information / calculation to the code which preceded it - such as parameters in a method.

Hope these quick thoughts help!

Hi Rick,

Thanks for the info…

1 Like

I had a really rough time with this video. Things were going pretty great up until the point where you seem to start juggling multiple abstract concepts at once. the State state; thing still throws me. I know you say that its ok to not get everything, but it can be pretty demoralizing to hear that ‘You don’t need to exactly understand how this works’ and in the next breath hear ‘this is the foundation we will build on’. :confused:

1 Like

Hey Sean,

Do you have a specific with regards to the above? If you ask, someone may be able to help. :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AdventureGame : MonoBehaviour {

   [SerializeField] Text textcomponent;
    [SerializeField] State startingState;

	// Use this for initialization
	void Start () {
        textcomponent.text = ("I AM AWESOME");
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

Assets/AdventureGame.cs(15,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?
I am getting this error…

Hi,

Are you sure that is the correct version of the code for the error message?

Looking at what you’ve posted, the error is on line 15, but that would be the comment above the Update method, which would make no sense. Please be sure to post the correct code (all of it) which maps to the error message you are receiving.


See also;

Hi,

I understand how states are used to return story text, but wondering if it’s also possible to use states to return images as well? I’m interested displaying a new image with each state (ex: moving into an office shows an image of an office, then moving into a living room shows an image of a living room).

I know how to add an image field to states and assign images to them, but haven’t quite figured out how to return that image in the game.

Hi Jiggly,

I know how to add an image field to states and assign images to them

You’re halfway there then.

All you need to add is effectively a place-holder on the UI Canvas which you will then populate with the image from the state. In the same way you get a reference to the UI Text GameObject in the scene to update the story text, do the same for the image.

So in the scene, add a UI Image GameObject to the Canvas, and then set the Image component’s sprite property in code;

image

Hope this helps :slight_smile:


See also;

  • Unity - Scripting API : Image

Thanks Rob!

I’m definitely missing something. Here’s what I have:

State.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject {

    [TextArea(10,14)] [SerializeField] string storyText;
    [SerializeField] State[] nextStates;
    [SerializeField] Sprite storyImage;

    public string GetStateStory()
    {
        return storyText;
    }

    public Sprite GetLocationImage()
    {
        return storyImage;
    }

    public State[] GetNextStates()
    {
        return nextStates;
    }
}

AdventureGame.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour {

    [SerializeField] State startingState;
    [SerializeField] Text textComponent;
    [SerializeField] Sprite imageComponent;

    State state;

	// Use this for initialization
	void Start () {
        state = startingState;
        textComponent.text = state.GetStateStory();
        imageComponent = state.GetLocationImage();
    }
	
	// Update is called once per frame
	void Update () {
        ManageState();
	}

    private void ManageState()
    {
        var nextStates = state.GetNextStates();
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            state = nextStates[0];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            state = nextStates[1];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            state = nextStates[2];
        }
        textComponent.text = state.GetStateStory();
        imageComponent = state.GetLocationImage();
    }
}

Not sure what I’m missing? Visual Studio doesn’t seem to like it when I type “imageComponent.sprite”…

Try changing its type to Image instead of Sprite, in AdventureGame.cs;

[SerializeField] Image imageComponent;

…and then where you have;

imageComponent = state.GetLocationImage();

…in both the Start and ManageState methods, change that to;

imageComponent.sprite = state.GetLocationImage();

Once you have this working it may be a good idea to rename the variables accordingly.

Perfect! Works like a charm. Thanks, Rob!

1 Like

You’re very welcome, happy to help :slight_smile:

Thank god I’m not the only one!
I reviewed the lesson and kept on pausing and rewinding. It gets clearer, but after understanding 75% of it I’ll be moving on to the next lessons as Rick encouraged us to.

Btw your post is from 2 months ago…please tell me you clearly understand those concepts now.

[SOLVED] Hi I have a problem in my code .
Can you help me,please!


Thanks

Oh i solved it, I just closed Visual code and re-opended it.

You were missing a semi-colon on line 9 of State.cs… I’m guessing you found that. :slight_smile:

I feel like I understand most of this context here. But I wanted to see if I was getting something understood that may have only been implied:

In 8:49 of this video, Rick is clarifying that the variable “State” allows us to access the class “State” in “State.cs”.

Does this mean that by declaring the variable “State” in “AdventureGame.cs”, we have turned the public class in “State.cs” as a whole into a variable?

As most people here, apparently, I’m also a bit confused by this lecture. Obviously I’ll keep going and probably all of these concepts will click later.

One of the things that got me confused was the “return” keyword and the need to create a method called GetStateStory. Couldn’t we just access startingState and print the variable storyText? Why the need to complicate and create a method whose only purpose is to go get the variable storyText? Maybe we are obliged to create methods in order to access things in other scripts, I don’t know…

Secondly, the class “State” also confused me a bit, but I’m pretty sure it’s because I’m not familiar with coding. You basically created a variable called “state” that is of type “State”, which apparently means it comes from the public class “State”, which you had created before, from scratch, with the State.cs script. Does this mean that, if I create a new script, I can create a variable of type “AdventureGame”, because I’ve made a public class with that name using the AdventureGame.cs file? I thought that the types of variables were “fixed things” that belonged to the system like bool, int, etc and that you could not create them.

  public string GetStateStory()
    {
        return storyText;
    }

What does this method do?

 [TextArea(14,10)] [SerializeField] string storyText;

What about this method?

  [SerializeField] Text textComponent;
   [SerializeField] State startingState;

What does serializefield do?

I know that a state is the current state of a game, like the text on the screen changing when you make a new decision but the code doesn’t make any sense at all to me conceptually. I am just copying what Rick is doing without understanding what is going on.

Hi jevin_randhawa1,

I’m a beginner but I think I can answer your question. The first method GetStateStory(); is returning the storyText variable when it is called. So whenever you call that method, whatever data is inside the storyText variable is being sent back to the place the method was called from.

The second and third methods you’re referencing are actually not methods at all. Those are variables. You are creating a string type variable called storyText, a Text type variable called textComponent, and a State type variable called startingState.

Lastly, [SerializeField] will allow those variables to be seen in the inspector within Unity. That way you can both see and adjust the variable from Unity, without having to go into the scripting inside Visual Basic.

Hope this helps you and anyone else with the same questions.

Privacy & Terms