These are all references to assets, not anything inside the scene itself.
Now, what I am curious about is there a way for me to do add.component in such a way that these preset variables come pre-attached? Because currently if I add this component to a gameobject, all of its variables come blank. (Script added using Add.Component)
Hang on, I’ve been trying slightly different variants and some times it gets added with these preset variables, but other times not?
Is there something I’m missing about Unity here in how it handles things? Is it a load issue?
Heh, I don’t quite know where to look these things up as I don’t have the proper terminology. So please ask if I am being unclear.
What you’re looking for is the import settings for scripts. I haven’t found anywhere they’re documented.
It looks like the import settings will only take effect if AddComponent is called from an editor script. Here’s a test setup:
Test, Test2:
public class Test : MonoBehaviour {
private static int cntr;
[HideInInspector]
public bool createTheThing = false;
void Update() {
if (createTheThing) {
createTheThing = false;
new GameObject("Update_" + cntr++).AddComponent<Test2>();
}
}
}
//in Test2.cs
public class Test2 : MonoBehaviour {
public GameObject g;
}
Editor:
[CustomEditor(typeof(Test))]
public class TestEditor : Editor {
private static int cntr;
private Test script;
void OnEnable() {
script = (Test) target;
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
if (GUILayout.Button("Add from Update")) {
script.createTheThing = true;
}
if (GUILayout.Button("Add from editor")) {
new GameObject("editor_" + cntr++).AddComponent<Test2>();
}
}
}
Assign some prefab to the import settings for Test2.g. if you click the “Add from editor” button, the new gameobject will have the prefab assigned to g. If you click the “Add from Update” button, g will be null.
I assume that means that the settings are stored somewhere that’s editor-only, which means that they’re not available in builds.
So it seems like it’s not possible to do what you’re asking for. You’ll have to either just add the things manually, or make some solution (like a ScriptableObject solution) where you bundle the component you want to add together with it’s values.
It depends on what you mean by “pre-set” values. Think of a GameObject as little more than a Transform plus a list of MonoBehaviour references. When you call AddComponent, Unity creates a new instance of the script and adds it to the list.
You can initialize the values in the component script, and then they’ll be pre-set when it gets created. I sort of doubt this is what you want but:
public class MyComponent : MonoBehaviour
{
public string Something = "preset value";
}
Every other option amounts to setting the values yourself after the fact. You can make it look like one line if you create an initialization method:
public class MyComponent : MonoBehaviour
{
public string Something;
public MyComponent Initialize(string value)
{
Something = value;
return this;
}
}
Attach it to a GameObject this way:
MyComponent c = gameObj.AddComponent<MyComponent>().Initialize("preset value");
This is exactly equivalent to doing it in two lines of code, but as you say, it’s for convenience:
MyComponent c = gameObj.AddComponent<MyComponent>();
c.Initialize("preset value");
And of course, if you’re always creating a GameObject with the same group of components, you should check out prefabs, if you’re not familiar with them. That’s exactly what they’re for.