- Home /
Attach scripts to instantiated objects?
This is an odd question, but when you instantiate objects, is there anyway to attatch bits of code (like the one below) to them?
function Update(){ transform.Translate(Vector3.forward 10 Time.deltaTime);}
I say odd, because I know I could just attach the script to the object in the inspector I just want to know this for organizations sake.
So I am asking if I can create/add a script as I instantiate a clone of an object.
Answer by Jessy · Jan 16, 2011 at 06:48 AM
You're asking about bits of code, and then you're asking about entire scripts. Please clarify what you actually want. There's probably some way to do the former, but I don't know about it, and it sounds like more complexity than you'd want anyway. If you want to add a script, use this:
http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html
Answer by Loius · Jan 16, 2011 at 07:04 AM
Technically yes -
You can use the eval command in Unityscript to run strings, so you could have eg
var injectableCode : String;
function Update() { eval( injectableCode ); }
But as eval is slow and it's extremely easy to mess up and not notice with strings, you should stick with AddComponent unless you positively absolutely have to use eval.
Your answer

Follow this Question
Related Questions
Problem with iTween and instantiated path 0 Answers
circular movement with force not working 1 Answer
2D enemy movement to target 1 Answer
Object movement - undesired effects 1 Answer