class constructors

I have a few questions that hopefully someone can answer.

What’s the difference between a public, normal, and private class? Also same for variables inside of the class being public, ect.

Is it okay for different public classes with public variables to have the same names for the variables?

Why am I unable to use yield WaitForSeconds in a constructor? I setup a Debuff class and gave it a duration for how long it should be active in my List before removing itself from the list. Not sure if this should be done some other way?

Also do while loops work in constructors?

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;

    Debug.Log(x.b);
}

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 a in 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 Start() or 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.

Cheers!

If write a public as a variable name that can be used total project.
Private variable can be used in single class only. Your not accessed that variable in another class.

For suppose: if u write code

public static float Timeperiod=0.0f;

if we declare like this that Timeperiod is used total scene.

private static float Timeperiod=0.0f;

if u declare like this but you want to use that timeperiod in some other class then that is not possible that is showing error like : inaccessible due to its protection level
so you must declare that as a public.

For static variables simply by using class name we can access that variable. for same class and that objects have one value. But if the variable is not static that object value changes.

Hey that’s it. sorry for my poor English.