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;
}
}
}
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?
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âŚ
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.