- Home /
difference betweeen public int a=0 and public int a; void Start(){ a=0; }
can anyone tell me what's the difference between these?
public int a = 1;
and
public int a;
void Start(){
a = 1;
}
and which one is better to increase the performance?????
Answer by Hellium · Dec 13, 2017 at 09:54 AM
Assigning a simple integer in Start or when you declare the attribute won't change the performances at all.
However, since your integer is public, it can be changed using the inspector.
Thus, assigning the value in the Start
method will override the value your designer (or you) have set in the inspector. When assigning the value when declaring the field, a
will have its default value set to 1 when you attach / reset the component.
There's a little more to it than that, because of the fact that Start() only runs before the first Update (not when "attaching a component" - I think you're thinking of Awake() there). The variable could be accessed before that. In fact there may never be a first Update, so Start() might never run.
This could have a huge impact (for example, within Awake(), or if one were instantiating an inactive prefab and doing some set-up on it before activating it).
I never said Start
runs when attaching the component. I said a
will be set to 1 if the following syntax is used public int a = 1;
, when the component is attached.
Thanks for clearing that up, have to admit I found that paragraph a little confusing.
yes I want to instantiate an object .but I don't know which is better
public int a = 1;
or
public int a;
void Start(){
a = 1;
}
,I know that Start will happen when you instantiate the object, but what about codes I wrote before start function? do they compile when object instantiates or what? :)
No, Start() will not happen when instantiating the object. That's my point. It happens before the first time Update is called and that might never happen. If you put the code in Awake() then it will always be called on an active component. But objects don't have to be active, so Awake() won't necessarily be called either.
Your public variable will initially take the value assigned in the inspector. You can change its value in Awake() or in Start() or in other functions. What's important is to understand when (and if) those functions are called and that the variable will still have the value you see in the inspector until you change it.
sorry I don't get it :P . need more to understand.you said Assigning a simple integer in Start or when you declare the attribute won't change the performances at all. what about an array of int?
int a;
a[0]=1;
a[1]=5;
.
.
.
a[25]=55;
should I do this inside Start function or declare it at first? like this
public int[] a=a[0]=1,a[1]=5,.....
When you have code in the Start
function, it will be run when your object has been enabled for the first time (when your scene starts usually)
If you use the declare + initialize syntax, the values are assigned when Unity creates the instance of the script (before Awake
).
So it's quite up to you, depending on your needs. But for simples values like integer, floats, strings, even small arrays, I strongly believe the performances will be the same.
I've got an array with 100 elements,so you think there's no difference between
public int[] a=new int[]{0,5,3,6,7,.......,7676};
and
public int[] a;
void Start(){
a[0]=1;
.
.
.
a[100]=12;
}
what about public or private? do they affect on performance??
Your answer
![](https://koobas.hobune.stream/wayback/20220612144511im_/https://answers.unity.com/themes/thub/images/avi.jpg)