# Learning To Use C# In Both VisualStudio And Unity's Monodevelop Frameworks

I apologize if this has been asked and answered, but I can't find it. I know the framework is different in MonoDevelop and Visual Studio 2010 Express. For instance, if I want to use Write.Line, I need to change the code to Debug.Log. Is there a source for crossover syntax? I found this but it doesn't seem to answer the question.

namespace ConsoleApplication1
{
class Program
{
static void Main()
{
string name;

Console.WriteLine("Hello, what is your name?");
Console.WriteLine("Your name is  " + name);
}
}
}


For instance, where would I find a resource to translate this code to MonoDevelop? I know Unity doesn't use static void, namespace, Console.ReadLine();, etc. Thanks for the help!

• Are you actually referring to the Main() function ? Static, void and namespace are C# keywords. There is no Main() in Unity, since you will be running only one application at a time. You have to put your code inside a script and attach it to a game object for it to be called. The only thing that is different is the Console class as far as I know, because Unity has its own kind of "console" class split in error, warning and format put together inside Debug class. – dimitris93 Mar 18 '15 at 23:34
• Off-topic, I highly recommend getting VS 2013 Community to replace Express! – Jon Mar 18 '15 at 23:48

The C# language is not changed between MonoDevelop and Visual Studio. You'd have access to Console if you included using System; in your MonoBehavior script, it just won't do what you expect because the Unity editor isn't a console application.

There's no direct conversion between the two because they are not replacements for each other. They may have similar functionality, but the Debug class is not a replacement for the Console class.

Instead of looking for a guide to convert between the two (which doesn't exist), look for the functions you want to perform in Unity.

This occurrs because, unlike a normal C# program, Unity does not have a Main() function. Instead, you should do the following.

• Start a new Unity Project

• Add a new empty GameObject to the scene

• Add a new script

• Double-click on the script and it will open in MonoDevelop.

Unity will generate a script with the following code:

class ScriptNameHere
{
void Start()
{

}
void Update()
{
}
}


This is where you will put all user-written code.

It's still C# but code written with Unity can only use .NET 3.5, so anything added from .NET 4.0 onwards won't work, but basic code like this should be fine.

Instead of using Console.WriteLine() and Console.ReadLine() you will need to read up on topics such as how to create a GUI in unity, how to render a text box, and how to process input, but to output to the Unity console you will use this code:

Debug.Log("Hello World");


There is no direct conversion, as others have stated, unity is not a console application, so writting and reading from something that is not there is not going to work.

This means you need to do all the heavy lifting yourself. The methodology here is going to be a great deal different than working with consoles, because Unity is handling all of the user inputs.

To accomplish this (or, one way to skin this cat---), I personally would recommend using an Update function (a function that you can write, that is automatically called once per frame on any monobehavior). Additionally, I would use Unity's Input class to listen for key strokes.

And this is what that might look like:

using UnityEngine;
using System.Collections;

public class sample : MonoBehaviour {

//Location where the name is stored while it is being typed.
private string name="";

//The different 'stages' in which we are interacting with the player.
private enum Stage{
SayHello,
WaitingForInput,
SayName,
Complete
}
//The current 'stage' right now. When this starts, it starts by saying hello
private Stage currentStage = Stage.SayHello;

//Every frame this is run. We need to keep track of the users input over many frames.
void Update () {

//Check which stage we are currently in.
switch (currentStage) {

//If we are in the sayHello Stage
case Stage.SayHello:
Debug.Log("Hello, what is your name?");
//Change to Wait for Input Stage. This is done after sayhello is run one time.
currentStage = Stage.WaitingForInput;
break;

//If we are in the waiting for input stage
case Stage.WaitingForInput:
//If the user has hit any keys on the keyboard this frame
if(Input.inputString.Length>0){
//if any of those keys are the return button
if(Input.inputString.Contains("\r")){
//you are done waiting for input, now you can process to say their name
currentStage = Stage.SayName;
}else{//otherwise
//add the input from this frame into the name variable for later use.
name += Input.inputString;
}
}
break;

//If we are in the sayname stage
case Stage.SayName:
//Say the name
Debug.Log("Your name is "+name);
//Change to a complete stage (a stage that does litterally nothing and is missing from this switch).
currentStage = Stage.Complete;
break;
}
}
}


So if you just take all that and put it in a c# script in unity, and attach it to any game object, that should do close to what you are looking for.

Your question is one of those questions that seems like it should be really simple, but unfortunately Unity and console applications are very different in many respects. Keep working at it, and in no time you will get the hang of it!

Hope that helps and good luck!

• I can't upvote, but thank you very much for the very thorough answer. I appreciate your example and all. Also, your politeness and words of encouragement. I will keep at it! Thanks! – Shelly Mar 20 '15 at 4:03