• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by aravynn · Jul 04, 2011 at 01:48 AM · instantiateprefabgunprocedural

inserting random gun into player/enemies hands

Hello,

I am recently new to unity, and I am trying to slowly learn everyt$$anonymous$$ng I'll need to know to make my game work. so for I have gotten pretty much everyt$$anonymous$$ng, except for 1 t$$anonymous$$ng.

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 w$$anonymous$$ch gun each unit will actually use.

I can figure out the coding of mostly everyt$$anonymous$$ng else, but im not sure how to actually add a weapon in t$$anonymous$$s fas$$anonymous$$on, 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 ( w$$anonymous$$ch 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 t$$anonymous$$s?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by aldonaletto · Jul 04, 2011 at 02:26 AM

If you want to have several different weapons and add to each one a variable number of accessories, I t$$anonymous$$nk 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 c$$anonymous$$lds. You should instantiate the prefab weapon of the selected type, then enable the selected accessories. The FPS tutorial uses t$$anonymous$$s method to select the weapon: all weapons are c$$anonymous$$lded 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 c$$anonymous$$lded to the object and randomly enable about 25% of them:

 for (var i=0;i<transform.c$$anonymous$$ldCount;i++){
   var activate = (Random.Range(0,100)<25);
   transform.GetC$$anonymous$$ld(i).gameObject.SetActiveRecursively(activate);
 }

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 t$$anonymous$$s modified one:

 firstWeapon = Instantiate(weapon23, position, rotation);
 for (var i=0;i<firstWeapon.transform.c$$anonymous$$ldCount;i++){
   if (Random.Range(0,100)>25){
     Destroy(firstWeapon.transform.GetC$$anonymous$$ld(i).gameObject);
   }
 }
 // then instantiate copies of firstWeapon
 weapon = Instantiate(firstWeapon, otherPos, otherRot);
Comment
DevonJavaScript

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SilverTabby · Jul 04, 2011 at 03:12 AM 0
Share

Quoting the original post:

"...without manually creating and pre-parenting each gun. ( there will be about 500 iterations of the various guns.) "

avatar image aldonaletto · Jul 04, 2011 at 01:25 PM 0
Share

I think @aravynn means "without manually creating 500 different prefabs" - the weapons must be created manually at design time anyway. Instead of creating one prefab for each possible combination of each weapon, my idea is to have the weapon prefabs complete with all accessories, and when instantiated disable the unwanted ones by script. If he needs 10 equal weapons with 2 or 3 specific accessories, for instance, he can instantiate the complete weapon prefab and eliminate the undesired features. Another similar approach - and maybe more efficient - could be simply to destroy the unwanted children (it sounds so cruel!)

avatar image

Answer by SilverTabby · Jul 04, 2011 at 03:13 AM

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){t$$anonymous$$s.transform.parent = target;}

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by Waz · Jul 04, 2011 at 01:58 PM

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!)

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Jul 04, 2011 at 06:15 PM 0
Share

I think the problem with adding accessories is the reference: any accessory prefab should be carefully adjusted in order to have the same pivot as the weapon, so it could be instantiated at the same position and rotation, and then childed to the weapon. That's why I think eliminating unwanted already installed accessories is easier than adding the desired ones.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate prefabs between 2 objects like a path 0 Answers

Procedural generation of prefab objects 1 Answer

Instantiate New Gun 1 Answer

Network.Instantiate a non-prefab? 1 Answer

Unity not Instantiating Prefabs Properly 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges