How can I include a script file inside another script?

I have a script that spawns objects and I want to include a script on the objects once they’re spawned on the scene. Is there a way I can do this?

Either add the script after spawning, or make the object a prefab with the scripts already attached.

can you tell me how to do this first one through script?

would this work?
public GameObject test;

void Start()
{
test.AddComponent<OVRTouchSample.OVRGrabbable>();
}

1 Like

Well…you have a script spawning objects already…

GameObject newObject = Instantiate(someObject);
newObject.AddComponent<SomeScript>();

Basic idea. When you instantiate, you get a reference to that newly spawned object and then just add the component to it.

1 Like

With that said - if you’re spawning prefabs why not just add the component to the prefab and save a line of code?

I often add scripts at runtime so that I can instantiate a ‘base’ prefab that can then perform different actions depending on which components get added.

1 Like