Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
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 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?

Comment
Add comment
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
1

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 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);
   transform.GetChild(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 this modified one:

 firstWeapon = Instantiate(weapon23, position, rotation);
 for (var i=0;i<firstWeapon.transform.childCount;i++){
   if (Random.Range(0,100)>25){
     Destroy(firstWeapon.transform.GetChild(i).gameObject);
   }
 }
 // then instantiate copies of firstWeapon
 weapon = Instantiate(firstWeapon, otherPos, otherRot);
Comment
Add comment · 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. Ins$$anonymous$$d 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 eli$$anonymous$$ate the undesired features. Another similar approach - and maybe more efficient - could be simply to destroy the unwanted children (it sounds so cruel!)

avatar image
0

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){this.transform.parent = target;}

Comment
Add comment · 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
0

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
Add comment · 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 eli$$anonymous$$ating 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.

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