Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 HexadimensionalerAlp · Aug 21, 2014 at 12:03 PM · c#inventoryequip

[C#] Equip weapon from Inventory

I want to make a weapon-equip system and here is my try:

     public Item currentWeapon;
 
     public Item.ItemMunitionType munitionType;
     public Transform playerHand;
     public Transform equippedeItem;
 
     public float reloadTime;
     public float fireRate;
     public float projectileSpeed;
 
     public int range;
     public int damage;
     public int roundsPerBurst;
 
     public bool canBurst;
     public bool fullAuto;
 
     if(Input.GetKeyDown("2"))
         {
             currentWeapon = inventoryScript.equippedPrimary;
 
             equippedeItem = currentWeapon.itemTransform;
             projectileSpeed = currentWeapon.itemProjectileSpeed;
             range = currentWeapon.itemRange;
             fireRate = currentWeapon.itemFireRate;
             reloadTime = currentWeapon.itemReloadeTime;
             damage = currentWeapon.itemDamage;
             roundsPerBurst = currentWeapon.itemRoundsPerBurst;
             canBurst = currentWeapon.itemCanBurst;
             fullAuto = currentWeapon.itemFullAuto;
             munitionType = currentWeapon.itemMunitionType;
 
             equippedTransform (equippedeItem);
         }
         
     }
 
     private void equippedTransform (Transform transform)
     {
         Transform selectedItem = Instantiate(transform, playerHand.position, Quaternion.identity) as Transform; 
         selectedItem.parent = playerHand;
         selectedItem.localPosition = Vector3.zero;
         selectedItem.localRotation = Quaternion.identity;
     }


Of course the "If(Input.GetKeyDown("2"))" is in the update void. The problem is that the Transform won't show up.

Item is a class I wrote but it's not necessary to know for that.

Comment
Add comment · Show 4
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 HexadimensionalerAlp · Aug 21, 2014 at 12:10 PM 0
Share

For your Information: It compiles just fine and I assigned the transform manually.

I forgot to mention that I assigned the inventoryScript in the start void.

avatar image Alessio89 · Aug 21, 2014 at 12:13 PM 0
Share

I'm not sure I understood what's the problem. Do you want your "Item" class to be exposed on the inspector? Or you want the Transforms to be exposed but they aren't?

avatar image HexadimensionalerAlp · Aug 22, 2014 at 07:04 AM 0
Share

I want the Transform of the object that I have currently equipped to show up in the scene(holstering a weapon for example)

avatar image HexadimensionalerAlp · Aug 23, 2014 at 08:34 AM 0
Share

http://answers.unity3d.com/questions/552598/equipting-a-sword.html

this guy had the same problem and did it like I did but for him it works. So where is the difference?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Joxno · Aug 21, 2014 at 12:47 PM

The reason is that you cannot instantiate a Transform, what you should be doing is instantiating a GameObject, which'll give you an empty gameobject with a transform that you can manipulate.

 private void equippedTransform (Transform transform)
     {
         GameObject selectedItem = new GameObject("SelectedItem");
         selectedItem.transform.position = playerHand.position;
         selectedItem.transform.rotation = Quaternion.identity;
         selectedItem.parent = playerHand;
         selectedItem.localPosition = Vector3.zero;
         selectedItem.localRotation = Quaternion.identity;
     }
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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't find an argument that corresponds to a formal parameter for a function 2 Answers

Button Listener void calling all prefab buttons in scene. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity "unequip" selected item from inventory. 1 Answer


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