For example,
Two Cubes. Cube 1 has an empty script component.
Cube 2 has a script with a method that Logs “Hello World!” to the console on every update.
After the cubes collide,
I want the script in Cube 1 to now contain that method.
Or,
In my project, I have a script with a method that Logs “Hello World!” to the console on every update.
After every 10 seconds,
I want the empty script in Cube 1 to append the method into the script.
So, after 100 seconds, the empty script in Cube 1 will now contain 10 “Hello World!” methods.
Thanks. Any help is appreciated!
What you’re specifically asking for is probably not possible.
However, you can very definitely add additional scripts to an object during runtime, using the AddComponent method. Using your collision example, you would do something like this:
// called when another object collides with this object
void OnCollisionEnter(Collision collision)
{
// add a component to this object
this.gameObject.AddComponent<SomeScriptClass>();
// add a component to the object that collided with this object
collision.gameObject.AddComponent<SomeScriptClass>();
}
You would likely want to check the other gameobject’s tags or layers to make sure you’ve hit the correct object, but in any case you can add any script or component you want to your objects during runtime, which is very useful