I'm not able to set default values by script (instead of inspector) because there is MeshRenderer
, a RigiBody
etc. These are not numerical values.
-
What is your problem? The fields are already set. – xoxox Jan 13 '16 at 22:08
2 Answers
Using the Reset
method instead of Start/Awake is a better way to initialize your component with default values (only if the default values are not related to game settings at runtime).
Use Awake
if you want to initialize your fields at runtime/gametime. And use Start
for all the GetComponent
logic. Because Start
is called after Awake
and makes sure that all other components in the scene are properly initialized before you call GetComponent
to find them.
Edit: Example monobehaviour with reset.
public class Player : MonoBehaviour
{
public string Name;
public float Health;
public float Armor;
void Reset()
{
Name = "UNKNOWN";
Health = 100;
Armor = 0;
}
}
Then add this component to a GameObject
in Unity. You'll notice these values with be automatically set. If you ever want to reset this component: right-click and reset.

- 1,316
- 1
- 12
- 24
-
Yes, `Reset` seems to be the way to go. Can you edit your answer with an example of `Reset` applied in a script? I'm not sure how to use it... – Jan 15 '16 at 13:03
Just like you can initialize a primitive value in the script you can also do it with objects like this:
public class ExampleScript : MonoBehaviour {
public int Velocity = 0;
public Vector2 Position = new Vector2(0, 0);
void Start(){
...
}
void Update(){
...
}
}
But you can only instantiate objects, if you need to modify it in some way you should use the Start/Awake methods.
Read more about this here: Initialize class fields in constructor or at declaration?

- 1
- 1

- 190
- 1
- 11