inserting random gun into player/enemies hands


I am recently new to unity, and I am trying to slowly learn everything I’ll need to know to make my game work. so for I have gotten pretty much everything, except for 1 thing.

I want to have the enemies guns be randomly generated, using code to randomly attach the components to the guns ( adding extras such as scopes and such ) and to randomly select which gun each unit will actually use.

I can figure out the coding of mostly everything else, but im not sure how to actually add a weapon in this fashion, without manually creating and pre-parenting each gun. ( there will be about 500 iterations of the various guns.) im really confused, since i cant seem to make an object parent under a prefab ( which is the issue im currently having. )

i thought about adding an empty game object under the hand joint, but how can i code the gun to instantiate in that, since wouldnt that be still in a prefab?

any ideas on this?

If you want to have several different weapons and add to each one a variable number of accessories, I think the best alternative is to have all accessories already added to each weapon prefab, and enable or disable itens using SetActiveRecursively - it can enable or disable the game object and all its childs. You should instantiate the prefab weapon of the selected type, then enable the selected accessories. The FPS tutorial uses this method to select the weapon: all weapons are childed to the player, but only one of them is active at a time - the others remain invisible and inactive (in your case the accessories would be acivated/deactivated). The code below is an example: it sweeps all accessories childed to the object and randomly enable about 25% of them:

for (var i=0;i<transform.childCount;i++){
  var activate = (Random.Range(0,100)<25);

EDITED: Another alternative is to simply delete the unwanted accessories right after the weapon is instantiated. You can instantiate one single weapon, eliminate the unwanted accessories and instantiate the others from this modified one:

firstWeapon = Instantiate(weapon23, position, rotation);
for (var i=0;i<firstWeapon.transform.childCount;i++){
  if (Random.Range(0,100)>25){
// then instantiate copies of firstWeapon
weapon = Instantiate(firstWeapon, otherPos, otherRot);

A quick glance at the Transform documentation tells me that if you are trying to parent one object to another from inside a script, you can just say transfrom.parent = target; and that should parent your gun/prefab/whatever to your hand/leg/whatever

Maybe try

createdObject.SendMessage("setParent", targetGameObject);

and then on a script in your prefab,

function setParent(target : Transform){this.transform.parent = target;}

Once your player is instantiated, it is not “in a prefab”. You can, from code, Instantiate other objects and parent them into place in the hands, and for that matter parent scopes onto rifles. As SilverTabby says, just set the parent:

createdObject.transform.parent = hand;

You probably then want to set the localPosition to zero and the localRotation to identity.

Unless you have a very small number of guns, pre-parenting will be hard to maintain (and with 500, I can see why you want to avoid it!)