Testing script failing in Unity Turn Based Strategy course

Hey - I’m going through the Unity Turn Based Strategy: Intermediate C# Coding course. I’m up to the Grid System video under the Grid System & Video category.

I have no compiling errors in VSC, but in Unity, I’m seeing this error:

“Assets\Scripts\Testing.cs(19,30): error CS1061: ‘GridSystem’ does not contain a definition for ‘GetGridPosition’ and no accessible extension method ‘GetGridPosition’ accepting a first argument of type ‘GridSystem’ could be found (are you missing a using directive or an assembly reference?)”

As far as I can tell, the code I’ve written out is identical to the video:

Testing script:
image

GridSystem script:

GridPosition script:
image

Not sure what’s going wrong here. Anyone able to put me out of my misery?

All looks fine. What does MouseWorld.GetPosition() return?

Hiya - sorry that’s the one script I missed off. Here:

That’s fine, too. Have you tried clearing the console and seeing if the error is still there? Unity likes to keep errors in the console - although they also may go away when it’s rebuilt the code. You could also right-click in a folder on the Project tab and select ‘Reimport’ from the context menu. This will let Unity rebuild the code. If it’s still an issue, the error will remain in the console. But I don’t see an error and if VSC is not telling you that there’s an issue, there probably isn’t and Unity is being stupid again

Thanks for going through it all, glad I’m not going mad. I did try rebooting Unity while I was having the problem and it didn’t seem to fix it.

Since it was a testing script, I decided to skip it and carry on with the tutorial last night, so I’d have to go back a few steps to see if it works now. However this will be really useful if I run into a similar issue again. Thanks for taking the time to reply.

EDIT: For folks viewing this later, it looks like something may indeed have gone wrong with my Testing script file, as when it was next used in the tutorial it caused random errors that shouldn’t have been there. I put the code on the clipboard, remade the file, pasted it in the new file, and suddenly the problem was gone, with no change to the code at all

If you’ve used GetGridPosition somewhere else without the issue, I wouldn’t worry about it. You can delete the Testing script and recreate it if you have to. You will be using it again later in the course, iirc

It looks like your debug line isn’t calling the Instance of MouseWorld so while VS sees the public function GetPosition, Unity doesn’t know where to look without feeding it the instance.

You have Debug.Log(gridSystem.GetGridPosition(MouseWorld.GetPosition());
It should be Debug.Log(gridSystem.GetGridPosition(MouseWorld.instance.GetPosition());

So I’m now on the LevelGrid video, and I’m getting a very similar problem. This time I can’t really skip it, and the solutions you posted above didn’t work, so I’m kinda stuck.

Here’s the error in Unity:

“Assets\Scripts\Unit.cs(46,32): error CS1061: ‘LevelGrid’ does not contain a definition for ‘UnitMovedGridPosition’ and no accessible extension method ‘UnitMovedGridPosition’ accepting a first argument of type ‘LevelGrid’ could be found (are you missing a using directive or an assembly reference?)”

Again, there’s nothing flagged in VSC and I’m pretty sure what I’ve typed is identical to the lecture, but I’ll screenshot it just in case:

Unit script:

LevelGrid script:

GridPosition script:

I also had a bizarre issue where I got a bunch of errors flagged by Unity at one point for code I had commented out, and I actually had to move the comment elsewhere in the file to stop it. I’m not sure if something buggy is going on

Okay so I fixed the problem, by moving a comment at the end of the Unit script.

…I have absolutely no idea why or how it’s even possible that a comment could mess up the game, but hey - working now

No. MouseWorld isn’t a singleton, so there is no instance property on it

1 Like

Yeah my bad, I misread the tiny image thinking it was a public static instance.

1 Like

I didn’t see anything in the script that would cause the issue. This could be Unity or VS Code misbehaving.

For future code pastes, rather than pasting in a screenshot of the code, paste in the code directly as text.

Here’s how to format it:
next to the 1 key on your keyboard is a backwards ` mark.
Before pasting the text of the script, on it’s own line, type that ` three times
```
paste in your code and then on it’s own line after the code do the three ` again.
```
This will format the code nicely, and make it much easier for some of us old folks to read. It also makes it easier to copy and paste the code to help with specific sections.

Thanks Brian - I’ll keep that in mind for next time, which might be in 5 minutes as I’m struggling to deal with the same error showing up yet again

Yup, same issue again, though it’s showing up on VSC this time at least. I’m a few minutes into the Move Action Validate video.

Error is:
“GridPosition’ does not contain a definition for ‘IsValidGridPosition’ and no accessible extension method ‘IsValidGridPosition’ accepting a first argument of type ‘GridPosition’ could be found (are you missing a using directive or an assembly reference?”

Again, I’m sure I have a perfect copy of the code from the course, but I’ll copy it below incase:

LevelGrid script (where the error is located):

public class LevelGrid : MonoBehaviour
{
    
    public static LevelGrid Instance {get; private set;}
    [SerializeField] private Transform gridDebugObjectPrefab;
    private GridSystem gridSystem;

    private void Awake() 
    {
        if (Instance != null)
        {
            Debug.LogError("There's more than one LevelGrid! " + transform + " - " + Instance);
            Destroy(gameObject);
            return;
        }
        Instance = this;

        gridSystem = new GridSystem(10, 10, 2f);
        gridSystem.CreateDebugObjects(gridDebugObjectPrefab);
    }

    public void AddUnitAtGridPosition(GridPosition gridPosition, Unit unit)
    {
        GridObject gridObject = gridSystem.GetGridObject(gridPosition);
        gridObject.AddUnit(unit);
    }


    public List<Unit> GetUnitListAtGridPosition(GridPosition gridPosition)
    {
        GridObject gridObject = gridSystem.GetGridObject(gridPosition);
        return gridObject.GetUnitList();
    }

    public void RemoveUnitAtGridPosition(GridPosition gridPosition, Unit unit)
    {
        GridObject gridObject = gridSystem.GetGridObject(gridPosition);
        gridObject.RemoveUnit(unit);
    }

    public void UnitMovedGridPosition(Unit unit, GridPosition fromGridPosition, GridPosition toGridPosition)
    {
        RemoveUnitAtGridPosition(fromGridPosition, unit);
        
        AddUnitAtGridPosition(toGridPosition, unit);
    }
    
    public GridPosition GetGridPosition(Vector3 worldPosition) => gridSystem.GetGridPosition(worldPosition);

    public bool IsValidGridPosition(GridPosition gridPosition) => gridPosition.IsValidGridPosition(gridPosition); \\\ Error here
}

Testing script:

public class Testing : MonoBehaviour
{
    [SerializeField] private Unit unit;

    private void Start()
    {

    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            unit.GetMoveAction().GetValidActionGridPositionList();
        }
    }
}

GridPosition script:

public struct GridPosition : IEquatable<GridPosition>
{
    public int x;
    public int z;

    public GridPosition(int x, int z)
    {
        this.x = x;
        this.z = z;
    }

    public override bool Equals(object obj)
    {
        return obj is GridPosition position &&
               x == position.x &&
               z == position.z;
    }

    public bool Equals(GridPosition other)
    {
        return this == other;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(x, z);
    }

    public override string ToString()
    {
        return $"x: {x}; z: {z}";
    }

    public static bool operator ==(GridPosition a, GridPosition b)
    {
        return a.x == b.x && a.z == b.z;
    }

    public static bool operator !=(GridPosition a, GridPosition b)
    {
        return !(a == b);
    }

    public static GridPosition operator +(GridPosition a, GridPosition b)
    {
        return new GridPosition(a.x + b.x, a.z + b.z);
    }

    public static GridPosition operator -(GridPosition a, GridPosition b)
    {
        return new GridPosition(a.x - b.x, a.z - b.z);
    }
}

Have been through everything three times getting increasingly frustrated and can’t see anything wrong with any of the scripts.

If this is a problem with Unity or VSC, how do I identify it and fix it?

Apologies - this one was actually me. I wrote position instead of system where the error is located

1 Like

That’s one of the trickier parts of scripting in general, remembering which script is responsible for which feature. In this case GridPosition, which is little more than a glorified structure, has no real knowledge of the outside word. GridSystem, on the other hand, is organized using GridPositions, and has knowledge of the boundaries of the world and which GridPositions are valid locations.

Privacy & Terms