hi i'm currently doing my MCPD in C# and kind of struggling to come to terms with C# in Unity3D. is there someone that can assist me as i'm not sure where to start.
i think i may be to used to .NET and not pure C#.
Answer by duck
Mar 16, 2010 at 02:22 PM
The C# in unity is regular C#, just like you'd learn when studying for your MCPD. If you're used to .Net, you'll be right at home going straight into C# in Unity. Of course, the more specific areas of .Net relating to Windows Forms & ASP applications don't really carry through to Unity, but all the general-purpose programming techniques you'll learn are very relevant and useful.
The only significant differences are:
While you can use Visual Studio for editing and scanning for compile-time errors, you have to actually build & run in the Unity IDE.
Unity is built on Mono which is an open-source implementation of .Net, so there are slight differences relating to that.
The current version of Unity (v2.6) uses Mono 1.2.5 (which is on par with .NET 2.0)
Unity doesn't currently have a debugger, although it does have a sort of pause-and-inspect mode. This means you often have to resort to tracing out statements to the console in Unity's editor using Debug.Log(). This situation is being improved - apparently we'll have real debugging in the recently announced version 3 of Unity. Yay!
Unity isn't thread-safe, so you can't use multithreaded code to directly interact with Unity objects.
So, when using C# in Unity, you have access to almost the whole .Net API (as supported by the version of Mono in Unity) - Which includes many things that people are sometimes surprised are available, such as FileIO, Collections, Delegates, Try/Catch, Sockets, StringBuilder, etc.
In addition you have access to the Unity Scripting API which you use for manipulating Unity-specific objects such as GameObjects, Components, Vectors, Quaternions, Physics Objects, Animations, as well as for other Unity-specific tasks like reading input from the Mouse, Keyboard and other controllers.
thanks for replying Duck, the prob is i wanted to read in file using filestream but had to use filinfo instead. can you please mail me some examples of using C#?
FileStream should work just fine in Unity (except if you're deploying to webplayer, for security reasons) What problem are you having?
Here's an example using FileStream to load images taken from the iPhone's camera into Textures in unity! http://forum.unity3d.com/viewtopic.php?t=29129
depends what you want to read and write. For small amounts of data saved and loaded locally, there's PlayerPrefs: http://answers.unity3d.com/questions/3218 otherwise you generally save and load by sending and receiving information via a server: http://answers.unity3d.com/questions/3560
i used this : System.IO.FileStream fs = new System.IO.FileStream (@"c:\text.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.streamReader sr = new System.IO.StreamReader(fs);
it gives me 3 errors: 1) field initializer cannot reference non static field. 2) StreamReader has invalid arguments. 3) cannot convert type object to system.io.stream.
how should i use it then?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
The name 'Joystick' does not denote a valid type ('not found')
Setting Scroll View Width GUILayout
Material doesn't have a color property '_Color'
How to import the object from server to unity