Hmmm…I know the word private’s effect,while I don’t really catch the word public’s effect.
public var i:int=0;
var j:int=0;// no public
Hmmm…I know the word private’s effect,while I don’t really catch the word public’s effect.
public var i:int=0;
var j:int=0;// no public
Thats because in US the variable declaration without an access qualifier is public ![]()
Public variables make sure it can be accessed from other scripts, just like statics only statics work a bit different. like this:
(other script)
public var i:int;
(other script)
function Start()
{
otherGameObject.transform.GetComponent(otherScript).i = 0;
}
that way you can change variables between scripts (and gameobjects)
the static variables only work in an easy way if you only use the script once (and not on multiple gameobjects)
they dont even have to be linked to an object
so… if in any script you can just do
ScriptName.DoSomething()
as long as DoSomething is an static function in ScriptName.
-edit
also i know i made alot of “grammar” mistakes and type naming mistakes but it’s really late here but you get what i mean dont you ![]()
No, public and static are two completely different things. Static means there’s only one instance of a variable. Static variables can be public or private; public static variables can be accessed from other scripts, private static variables cannot. Static is not a “shortcut” for avoiding GetComponent; don’t use it unless you specifically mean there should only be one instance of that variable.
As for the original question, just to be clear, “public var” and “var” are the same thing. Public is the default in JS (and private is the default in C#).
–Eric
Thank you very much.I get a lot from what you said.Hmmm…Thank you all of you.And sorry to late to reply.
yeah that’s what i ment sort of, already said it was late in the night ![]()
public + unityscript is like private + c# - its in both cases the ‘automatically done thing anyway’ but having it there helps the readability if you try to find public / private variables or want to have consistent formating
3/10 troll for making me reply…
public/privates have nothing to do with coding language…
also it’s not just for readability, each comes with their own positive and negative points as public and static is more intensive then private
furthermore, i hardly think it’s an “automatically done thing anyway” as most of the times you realize an variable needs to be public in the middle of the script as you may need other scripts to correctly help it…
What’re you talking about? The only point dreamora was making was that if you don’t declare the visibility of a variable in JS then it defaults to public. If you do the same in C# it defaults to private. The languages behave differently which is an important note for people making the switch from one language to the other (or just reading code in the language they are less familiar with).
You sound more like a troll.
It is language specific. In C# the default accessor is private, that’s if you don’t define an accessor, the variable/Method is private (with some exceptions: interfaces declarations are always public for example. They can not be protected or private). The default accessor in UnityScript is public.
//UnityScript
var someVariable : int; // It's public variable
// C#
int someVariable; // it's private
When you always add the accessor, it becomes more readable, because it’s specified
//UnityScript
public var someVariable : int;
// C#
private int someVariable;
This is much better readable and mistakes/misreadings are impossible. Readability != shorter code
8/10 @failed troll attempt ![]()
ah like that… I thought he ment something completely different… sorry guys ![]()