What does making a script public allow me to do?
I understand that public static scripts can be called by ScripName.PublicVar/Function but what does the public part do on its own?
What does making a script public allow me to do?
I understand that public static scripts can be called by ScripName.PublicVar/Function but what does the public part do on its own?
Static doesn’t have anything to do with access permission, it means there is only one instance of it. Public means it can be accessed by anything, private means it can only be accessed by that class.
–Eric
Hey John,
Marking a method or property as “Public” means that it is callable from “outside”. So if A.Test() is marked as public, then class B can call it. If A.Test() is marked as private, then class B can’t call it.
You may want to learn c# first before tackling Unity. A good place to learn would be http://www.csharp-station.com/Tutorial.aspx
Hope that helps!
El Diablo
Is there such a thing as Static Protected? So there is only one value… and your not changing it?
Diablo, when you set up a "public " variable in C#, and access it from another script (and therefore) another class, in Unity, you get an error message along the likes of “An object reference is required to access non-static member”. so unless there’s some special way of accesing it (besides scriptOrClassName.variableName) or other word to add to public… let us know.
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
–Eric
Hey eTag96,
The problem that you’re having has nothing to do with it being public; rather, you are trying to access an instance member via a type. This may sound like nonsense to you, so let me explain :
In order to create a class in c#, you need to do the following :
class MyClass
{
public static int MyStaticInt;
public int MyInstanceInt = 7;
private int MyPrivateInstanceInt = 666;
}
We’ve now created a Type called “MyClass”, and this type is a class. We could have made the type an interface, or a struct, but we chose the class keyword, so it’s a class.
So let’s create an instance of MyClass and try to access MyPrivateInt :
MyClass myClassInstance = new MyClass();
int value = myClassInstance.MyPrivateInt; // error
As you can see, this will generate an error, and the reason why is that we marked the field as private, and therefore we can’t access it from the “outside”. Because it’s set to private, it’s visible only to myClassInstance and nothing outside of it.
Okay, now let’s create 3 instances of MyClass and set their public MyInstanceInt fields to different values :
MyClass myClass1 = new MyClass() { MyInstanceInt = 10 };
MyClass myClass2 = new MyClass() { MyInstanceInt = 20 };
MyClass myClass3 = new MyClass() { MyInstanceInt = 30 };
Now we have three completely separate instances of MyClass in memory, with one containing the number 10, the other containing the number 20, and the other containing the number 30. Since MyInstanceInt has been declared “public”, I can access them from the outside, like so :
int value = myClass2.MyInstanceInt; // value should now have the number 20
However, what you’re trying to do is the equivalent to the following :
int value = MyClass.MyInstanceInt; // this will give you an error
You can’t do this, and will result in an error. The reason why you can’t do this should now be obvious : if you were allowed to do this, what value should be returned? 10? 20? 30? There are three instances of MyClass in memory; how does the computer know which one of them you want to get the value from? It has no idea, and the only way it would know is if you specifically specify the instance, which is either myClass1, myClass2, or myClass3.
Now, having said that, there is a way to store information in MyClass itself, and that is through the static keyword. By using the static keyword you are telling the computer that the following field/method pertains ONLY to MyClass, as was done with the MyStaticInt field. In other words, unlike MyInstanceInt, which has a unique value for each instance, MyStaticInt will have only one value not matter how many instances you make, even if you have zero or a million instances of MyClass.
You can refer to MyStaticInt as follows :
int value = MyClass.MyStaticInt; // value should be 666
Remember, MyStaticInt belongs to the “type” MyClass, not to any “instances” of MyClass. Therefore, you can’t do the following :
int value = myClass1.MyStaticInt; // error
Does this make any sense? I hope this cleared up some of the confusion. In any case, if there was any doubt regarding what I just explained, then you definitely need to learn C# since we are touching on some very basic stuff that you need to know before proceeding. Check out that tutorial link : http://www.csharp-station.com/Tutorial.aspx
Hope that helps!
El Diablo
thanks, I know how to make code, i just didn’t properly know what “static” and “public” did or why they were used, i just saw them once in a script, and guessed that was just necessary. but now I know.
Yes. Only 1 instance of it exists and it’s accessible to the class and any class that extends it. Also, you can change a static value whenever you want. Maybe you’re thinking of the ‘final’ keyword?
I don’t believe my question was answered (my question probably wasn’t clear).
If you make a member or method public, you can access through
GameObject.GetComponent().variable/function
but would making the class public give the trait to all of its variables and functions also?
Classes in Unity are always public, unless they’re nested inside another class.
Thank you, then I am going to make the assumption that making a class public does nothing more than it currently does.
Hey John,
Setting a class to Public makes the class itself accessible to others; it does not make its members public.
El Diablo