- Home /
CSharp Classes = Scripts? Difference between private string and normal string?
I just started learning CSharp / C# but I wanted to know if classes are basically scripts. Also, if they are, why do people make ten different scripts when they can use one script with ten classes? Also, what is the difference between private string and normal string (ex: string asdf;)? What is a vector? And vector3? What do use new for?
Answer by darthbator · Sep 10, 2013 at 12:30 AM
You can jam 12 classes into 1 file if you would like but that's super messy, hard to work with, and likely won't function if you are deriving from MonoGame in order to have you scripts participate in the component system inside unity.
When I first started this helped me a lot. A class is a "folder" and contains methods which are basically "files". A class holds a number of methods that are logically grouped together (and often depend on one another).
People encapsulate into 1 script -> 1 file because it makes sense.
The exposure level of a parameter defines what can access it. A public parameter can be accessed from classes that inherit from the base class (the one the public var is declared in), or from other objects accessing the parameter through the component system.
Hey, it's a long journey. I suggest a nice book on C#.
scripts are .cs files when you think of C#
a file can only contain one mono behavior which must have the same name as the class. This is why you need multiple files for multiple classes. It also makes it easier to know where your classes are. You might need a player object to have 2 mono behaviors, so you would place the two different .cs files on the player prefab in the inspector.
private strings can only be accessed inside the scope of that class. Conversely, if you have reference to a class with a public string, you can access all the public strings. You won't be able to see the private strings from that other class.
Also, in Unity, public strings show up on the object in the Unity inspector whereas private strings do not
I should add that it is generally good practice to keep as many variables private as possible. This prevents them from being accidentally modified in future, and helps keep your project clean.
This answer was helpful, but the other one was more understandable. Sorry! :P But I thumbed you up. :D