static vs public ?

I now use javascript.

Question 1. What is difference with static and public?

Is public also used in unity-javascript?

I normally use static.

But, someone says that static is remain to memory, so that application will be slowed. Right?

Question 2. Then, less static is more good?

I’m guessing you mean static vs public variables, because classes can be static or public too. But difference between the variables are that static variables are shared between all instances of same class and public aren’t. So let’s say you have a class “Example” and it has a static variable “varA”.

Example e1 = new Example();
Example e2 = new Example();

in this example

e1.varA

and

e2.varA

refers to same thing. If you change varA in one instance the other will be changed too.

No, that’s not the main problem with static variables. The problem is, they are global variables. Which is fine if you need global variables and have only one instance of the script that declares them, otherwise you are asking for trouble.

Here is an example. Create the following script, save it, add it to two different game objects (preferably two objects with different names), and in the inspector assign the first object’s number var to 1, the other to 2.

var number : int;
static var globalNumber : int;

function Awake(){
	globalNumber = number;	
}

function Update(){
	Debug.Log(this.name + ": " + globalNumber);	
}

When you run the code and view the results in the console, you’ll see something like:

Cube1: 1
Cube2: 1

This is because globalNumber was declared static, and all objects that use that script share that variable. If you attempt to assign that variable for one object, it will change the variable for all objects that use that script.

There are no performance implications for you to consider when using static vs instance level variables. It is purely your program architecture that drives which kind of variables to use.

Thanks for notice.

I experimented, and it is, as hierarchy’s order, all printed to 1 or 2.

Then what is solution?

The solution is to not use static variables unless it makes sense to do so.

If you have a score variable in a master script that there is one instance of, it makes sense to declare it static for ease of access from other scripts:

Master.score += 50;

Otherwise, use public variables. If you need one script to communicate with another, use BroadcastMessage() or GameObject.Find() along with GetComponent(). More info below.

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Is public variables means like below?

[script : A]
public var ABC : int = 10;

and then

[script : B]
A.ABC = 20;

?

You don’t need to declare it as public, it’s implied:

var number = 10; //public variable

No, you can’t access public variables this way. See the link I posted above.

Thanks!

I see now.
I didn’t know public is implied.

Then,

public var is same with
(just) var

Your link info is already I know and printed it, I always refer it.

In JavaScript yes, it’s implied
In C# it’s implied as private.

So is better to add public or private for no confusion and better understanding.