- Home /
How do I assign a script to an instantiated prefab?
I am currently creating a "spell" particle effect prefab which I need to turn into a Rigidbody to control it's physics to launch it through the use of an external script, the script itself is ready and setup but I am not sure how to assign that script to the said instantiated prefab.
Current code to spawn object:
GameObject go1;
spellFired = spellMechanics.testSpell1;
go1=Instantiate(spellFired,spellSpawn1.position, spellSpawn1.rotation) as GameObject;
go1.transform.parent=transform;
Just simple code I did to test out some different things, how would I go about assigning a script that is in my project folders (does it have to be within the Scene existing somewhere?)
Thanks in advance. Cheers.
Answer by TobyXD · Jul 09, 2013 at 09:35 AM
Maybe you can try GameObject.AddComponent.
ScriptClass myScript = gameObject.AddComponent<ScriptClass>();
@jaakaappi Sorry about that, but your post didn't appear when I post $$anonymous$$e :(
No problem, this system is a bit tricky and sometimes it doesn't show stuff like it should.
Answer by jaakaappi · Jul 09, 2013 at 08:28 AM
You can use the GameObject.AddComponent - function.
The script doesn't have to be anywhere in the scene, if it is included in your Unity project, the compiler will find it.
--jaakaappi
I've tried and it seems to do absolutely nothing, I've read up on it and I think I'm messing it up somehow just been up Unity-ing for 8 hours so maybe need to clear my head.
Okay. If don't get it to work could you post your code so I can see if the problem is in there?
I'll respond tomorrow when I double-check everything on a fresh head, if I don't respond it's all good.
Thank you =)
Your answer
Follow this Question
Related Questions
Assign a 'Transform' target to prefab by finding a 'GameObject' 1 Answer
Acessing scripts in other game objects with prefabs, without using GameObject.Find 1 Answer
Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers
Counter for sprite under script from GameObject prefab 0 Answers
Get unity to recognize prefab in C# 2 Answers