- Home /
Prefab and creating objects in script help
Okay I have a few questions about prefabs and creating duplicates in script. 1. I've made a prefab in the project tab, but does there have to be one instance of the prefab in the hierarchy? 2. How do I actually find the prefab in script? All the unity manual examples show duplication of the object that the script is in, but I want to duplicate on object that does not have the script in. 3. I'm supposing you set the prefab to a variable and then use Instantiate(avariable,Vector3,Quaternion.identity); Please correct me if I'm wrong. 4. My object has an animation that is set to play automatically. Will duplicating it reset the animation or will it also duplicate the time in the animation that it is at? In this case I'm just supposing that I'll have to change it to activate using script but I'd rather know ahead of time. Thanks for any answers, sorry if this is all too much for a single question, but I feel pretty straight forward about what I need to know at this point. Sorry if I'm missing any important information, I'm still new to scripting.
Answer by ArkaneX · Aug 10, 2013 at 10:04 AM
1) No.
2) You can load a prefab at runtime using Resources.Load or you can create a public field in your script, e.g.
public Transform SomePrefab;
and using it in Instantiate method. To assign some prefab to it, click a script in project explorer, it will show in Inspector windows and you will notice "Some Prefab" on top of it. Drag any prefab from project explorer to it and that's all.
3) Yes, though you can use other overloads as well.
4) This I don't know, but should be very easy to check.
Complete description of steps required to instantiate prefab is available here: Instantiating prefabs.
Thanks a lot! Looks like I was mixing up Javascript with C# as well =/ But thanks for all the help it makes sense now.
Your answer
Follow this Question
Related Questions
How would i instantiate a prefab in the animation curve event? 1 Answer
Component on animated object while keeping the prefab 2 Answers
VUFORIA: Playing an animation for a instanced model? 2 Answers
Trying to assign GUIText from heirarchy into instantiated prefab 1 Answer
Can't move instantiated prefab 2 Answers