|
example: I have a script that instantiate a weapon.
How can i pass prefab in SetWeapon(); or if i have something like this:
I know how to assign prefab from inspector, but how to change it from runtime. And if it is possible I like to avoid my weapons siting in the scene and setting weapon variable by gameObject.Find(); Thanks in advance :P
(comments are locked)
|
|
You can do this by making use of the Resources Folder. From the example on the Resources.Load scripting reference:
However Why do you want to do this? It might be a good idea if you stated your broader goal - why you want to avoid prefab references - because it might help someone give a more helpful answer. There are many reasons why prefab references are a good way to organise your project, and trying to bypass the way Unity is designed to work is generally not a good idea unless you have a specific reason that warrants it. Thank you for the answer. I was thinking to call SetWeapon() with the name of the prefab as a parameter and then menage the destruction of the previous weapon and instantiating of the new one in the body of the function. That way i can swap the weapon with a call of single function. And when i think about it again. I can do the same by assigning the same script with different script links in the inspector on the buttons that change the weapon. Resources.Load solve the problem the way I imagine it should be solved.And i am not sure that is the best way hehehe thank you again ;)
Oct 12 '10 at 09:49 AM
beloto
Yes, this isn't the best way. What you describe doesn't warrant the use of "Resources". You could use a Hashtable if you specifically want to reference your weapons by name, but typically this isn't necessary. I'd suggest using a "weapons" array, in which you can place a reference to every kind of weapon prefab, and then pulling out items from that array for instantiation.
Oct 12 '10 at 11:48 AM
duck ♦♦
Ah yeah array. that will be great way to house all the prefabs in one variable - place. and then i can pass separate index in the http://function.It will do exactly what i need. I already make solution without Resources.Load() where i put the same script on a different skill button, but linked different prefabs. In that way I reused the same script. Thank you for the array idea, that will work great also.
Oct 12 '10 at 02:46 PM
beloto
(comments are locked)
|
here is the solution that i make when i rethink the problem. Big thanks to Duck for helping me come with this solution.
(comments are locked)
|
