What is the Vector2Int gridPos; doing in the Waypoint.cs?

Hi,

Can someone please explain me where the Vector2Int gridPos; from the Waypoint.cs is being used ??
It looks like an unused variable to me ???

    public class Waypoint : MonoBehaviour    {

    Vector2Int gridPos;

    const int gridSize = 10;

    public int GetGridSize()
    {
        return gridSize;
    }
	
    public Vector2Int GetGridPos()
    {
        return new Vector2Int(
            Mathf.RoundToInt(transform.position.x / gridSize),
            Mathf.RoundToInt(transform.position.z / gridSize)
        );
    }

    public void SetTopColor(Color color)
    {
        MeshRenderer topMeshRenderer = transform.Find("Top").GetComponent<MeshRenderer>();
        topMeshRenderer.material.color = color;
    }
}

Or is it a place holder for this part:

public Vector2Int GetGridPos()
{
    return new Vector2Int(
        Mathf.RoundToInt(transform.position.x / gridSize),
        Mathf.RoundToInt(transform.position.z / gridSize)
    );
}

Does the new Vector2Int get assigned to the gridPos on top ??

To me it seems the variable on top should just be removed …

In the final lesson’s Waypoint.cs the line is still there …

Hi @Mellow,

It looks like a left-over that shouldn’t be there. Being that the lecture you’ve posted under is called instance variables, perhaps it was used to demonstrate something and then just wasn’t cleared up afterwards.

I’ve had a look at the GitHub respository and that line is there too, which suggests by the end of the Realm Rush section it isn’t removed.

It is a private member variable, so nothing outside of the Waypoint class is going to be able to directly access it.

If I were in your situation I would, now that you suspect it to be unnecessary, comment it out - then proceed to the end of this section, if everything is still working as you would expect, you can remove the line without any issues.

Incidentally, if you use Visual Studio, it will often tell you when variables are unused. :slight_smile:

Hope this helps :slight_smile:

1 Like

Hey @Rob,

Thanks for the answer :slight_smile:

I was wondering why I didn’t get an error about it in Unity, but it seems that you only get this error if a value is assigned to the variable, but it only says Vector2Int gridPos; so it won’t log an error.

I’ll comment it out and hope for the best !

1 Like

Hi,

Are you using Visual Studio or MonoDevelop? The former should certainly be able to provide the information/warnings about unused variables, fairly confident mine actually underlines them too.

Visual Studio 2017

But if I put for example " int blabla; " or any var - unity / VS will not log it.
If I say int blabla = 10; then it will produce the error about the unused variable.

The error is “The private field Waypoint.blabla is assigned but it’s value is never used”.
Seems if you don’t give a var a value it’s not error logged…

Thats probably why Ben missed it :wink:

Just had a quick fiddle with this, looks like if the variables are within a method it does report them, but if they are member variables it doesn’t, at least by default.

image

There are of course many extensions/tools you can download/install for free which may provide this support if Visual Studio doesn’t by default, although that would be surprising.

That’s good to know, thanks !

1 Like

No worries - I’ll keep digging regarding member variables :slight_smile:


Updated Tue May 01 2018 19:38

Found this article which suggests it was a decision based on user feedback to not produce these warnings;

1 Like

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

Privacy & Terms