How to add a script to an asset through script

Hello, I am working on a project where I need to add a script to an asset through script Here is my attempt:

		VL_TextureGrabber s1 = (VL_TextureGrabber)GetComponent(typeof(VL_TextureGrabber));
		AssetDatabase.CreateAsset(s1, "Assets/Chunks/" + m1.name + ".asset");

This does not seem to work and neither does any other way I have tried. Anyone have a solution?

EDIT
Here is the code I have both working and not working

//Get the MeshFilter Off of This GameObject(works)
		Mesh m1 = this.GetComponent<MeshFilter>().mesh;

		//Get the MeshRenderer Off of This GameObject(works)
		Renderer m2 = this.GetComponent<MeshRenderer>().renderer;

		//Get the Script Off of This GameObject(unclear if this is issue but is related to the problem)
		VL_TextureGrabber s1 = (VL_TextureGrabber)GetComponent(typeof(VL_TextureGrabber));

		//Create an Asset with the Name of m1 Using the m1(meshfilter) Saves fine
		AssetDatabase.CreateAsset(m1, "Assets/Chunks/" + m1.name + ".asset");

		//Add the MeshRenderer to the Created Asset works fine)
		AssetDatabase.AddObjectToAsset(m2, "Assets/Chunks/" + m1.name + ".asset");

		//Add the Script to the Created Asset(unclear if this is issue but is related to the problem)
		AssetDatabase.AddObjectToAsset(s1, "Assets/Chunks/" + m1.name + ".asset");

your two lines of code are seemingly unrelated and you don’t say which part doesn’t work but at least you used code tags :wink:

your CreateAsset call doesn’t use s1 in any way, even though it’s not clear what that is supposed to be.

if m1 is your asset, then to add a script to it, since it’s a component like anything else, you’d do the following:

m1.AddComponent<scriptName>();

then create the asset… you’re hopefully already aware, but this will only work in the editor, not at runtime.