C# Object Reference required to Access Error

In my console project, I get this error,

on the line 25 in this method.

private static void ReciveInput()

{
PrintOptions();
Vectormovement = newVector(0,0);
stringstr = Console.ReadLine();

if (str == "N")
{
movement.y = 1;
}
elseif (str == "E")
{
movement.x = 1;
}
elseif(str == "S")
{
movement.y = -1;
}
elseif(str == "W")
{
movement.x = -1;
}
VectornewLocation = newVector(player.GetLocation().x+ movement.x, player.GetLocation().y + movement.y);
}

player is a instance of the Character class and here is the class.

using System.Collections.Generic;
using System;

namespace SimpleTextAdventure
{
    public class Character
    {
        List<string> items = new List<string>();
        Vector location = new Vector(2,2);

        public Character ()
        {
        }

        public Vector GetLocation()
        {
            return location;
        }
    }
}

Why does this error occur?

ReceiveInput() has no access to your player variable. Are you sure you intended to declare ReceiveInput() as static?

What does declaring a function static do? When I remove static I get the same error when I try to call the function from the constructor of main. Can I call a class’s functions from its constructor?

Here’s my main file.

using System;
namespace SimpleTextAdventure
{
    class MainClass
    {
        Character player = new Character();
        Map map = new Map();

        public static void Main (string[] args)
        {
            //

        }

        private void ReciveInput()
        {
            PrintOptions();

            Vector movement = new Vector(0,0);
            string str = Console.ReadLine();

            if (str == "N")
            {
                movement.y = 1;
            }
            else if (str == "E")
            {
                movement.x = 1;
            }
            else if(str == "S")
            {
                movement.y = -1;
            }
            else if(str == "W")
            {
                movement.x = -1;
            }
            Console.WriteLine(movement.y);
            Vector newLocation = new Vector(player.GetLocation().x+ movement.x, player.GetLocation().y + movement.y);
        }

        private static void PrintOptions()
        {
            Console.WriteLine("Movement: N , E , S , W");
        }
    }
}

To answer your question, ‘static’ means that a function exists as a part of a class, as opposed to a part of the instance of a class. There is only one ‘copy’ of a static variable or function, instead of one for each instance like a normal function.

…and then my question - why are you using public static void Main? You don’t use that in Unity. Did you just grab some C# sample code from somewhere, I’m guessing?

1 Like

you appear to be trying to use the same approach as you would writing a game from scratch without any framework rather than working with the unity engine… :eyes:

edit: how unity does things: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

That makes sense. This is a Console Project I’m working on in Mono Develop in order to get familiar with C# enough to then go into “Unity”. Am I right to get familiar with C# before diving into the Unity editor and all that?

Thanks for the link. I’ll come back to that a bit farther down the road.

A good deal of the things you’ll learn in general C# are going to be irrelevant to Unity coding, and this post is a prime example. In fact, if anything, the reverse is true: learning the way you use C# in Unity will be a better springboard to learning general-purpose C#, than learning general-purpose C# would be in learning Unity - particularly for beginners. Unity makes it pretty easy to learn - fast compilation and immediate, visual feedback are excellent teaching tools.

Good to know. I’m learning very basic C#, mostly syntax for conditionals, variable declaration, loops. Stuff I won’t regret learning even if Unity doesn’t involve them.

Most of the C# syntax that’s relevant to Unity is covered in Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

Thanks, I’ll look through that.