What’s the difference between a public, normal, and private class? Also same for variables inside of the class being public, ect.
In other languages, like C#, public and private describe what other objects can have access to the class, and therefore what can make instances of it. Even though Unityscript allows you to put these access modifiers on the class definition, they appear to do nothing for the time being.
Now, public and private access modifiers for variables do work in Unityscript. These also define whether outside code can access the variables. Take for instance this scenario:
public class X
var a : float;
private var b : int;
function Start ()
var x : X = new X();
x.a = 34.56;
Because ‘b’ is marked as private, you will get this error:
'X.b' is inaccessible due to its protection level.
This allows you as the programmer to define internal data that gets used by the class, but other parts of code aren’t aware of it, and often times don’t need to be aware of it. Think about it like this: do you need to know how what the insides of a lamp look like to know how to plug it in and flip the switch? You don’t need to know those things. They are restricted from you, but that restriction does not interfere with using it to light a room.
Is it okay for different public classes with public variables to have the same names for the variables?
Yes. All the questions you are asking are all a part of a programing concept known as encapsulation. This means that a class is considered a capsule, because all data and functions within are bundled together into an idea. They all work together to convey this idea. Any variables that are similar or named the same in different classes are seen as separate because the class is their boundary or domain. Furthermore, the compiler can determine which variable you are referring to because of how its being used. In the example above, the compiler knows
x.a refers to variable
class X because x is of type X.
Why am I unable to use yield WaitForSeconds in a constructor?
This is an issue with how Unity is designed. The language you use, whether it’s Unityscript or C#, only facilitates the framework that Unity has set up for itself. WaitForSeconds is not something that came with the language. It is a function that was built for use with the Unity framework. Each object you create is not ready to be processed by the framework right upon construction. This is why Unity provides certain functions, such as Start() and Awake(), to allow users to do something with an object when the object is finally ready to be used in the framework.
Not sure if this should be done some other way?
Try setting up the
WaitForSeconds() in either the
Awake() function instead.
Also do while loops work in constructors?
Loops and constructors are both language constructs, so this is possible.
All that said, you might want to look pick up a book on object-oriented programming; something with a pace you’re comfortable with, where you can read the first chapters and learn some object-oriented concepts.