how to modify a script variable before adding to a game object

I have a outline script that I attach then modify it fields to output different effects as follow

void Start()
    {
        gameObject.AddComponent<Outline>();
        gameObject.GetComponent<Outline>().OutlineMode = Outline.Mode.OutlineVisible;
        gameObject.GetComponent<Outline>().OutlineColor = Color.green;
        gameObject.GetComponent<Outline>().OutlineWidth = 0.5f;
    }

but this only took effect after the script is attached and in many cases I have seen it first display the default outline then change to the desire output. Is there a way to attach the script with the field already defined?
Thanks in advance

1 Like

You can’t do that. Component can’t exist without GameObject (sadly).
There is no reason to make configuration in script. You can added component in editor, set all settings and enable in Start if you want. You can also use prefab with script and predefined settings and instantiate it.

You have to use AddComponent to create an instance of the script.
I’m guessing Outline is Unity’s outline component?

My suggestion is to add Outline as a component to the gameobject in the editor and just disable it. Then, instead of using AddComponent, just use GetComponent to grab the component, set it up and then enable it.

Are you using this to outline text?