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
1
Question by xanderdood · Dec 11, 2011 at 06:22 PM · multipleweapons

multiple weapons at run time

hi, im currently working on this code

var weapons: GameObject[]; private var curWeapon: GameObject;

function ChangeWeapon(weapon:int){ if (curWeapon){ Destroy(curWeapon); } curWeapon = Instantiate(weapons[curWeapon],transform.position,transform.rotation); }

function Update(){

if (Input.GetKeyDown("1")) ChangeWeapon(0); if (Input.GetKeyDown("2")) ChangeWeapon(1); if (Input.GetKeyDown("3")) ChangeWeapon(2); }

// FIN

Basically i am trying to change between weapons when a key is pressed, however i keep getting this error

" InvalidCastException: Cannot cast from source type to destination type. Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) weapons.ChangeWeapon (Int32 weapon) (at Assets/scripts/enemy/score/weapons.js:10) weapons.Update () (at Assets/scripts/enemy/score/weapons.js:16) "

any ideas? thankyou

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Dec 11, 2011 at 07:01 PM

EDITED: You're using curWeapon as an index, but it's a GameObject. Like @Bunny83 said, you should use weapon instead, like below:

var weapons: GameObject[]; private var curWeapon: GameObject;

function ChangeWeapon(weapon:int){ if (curWeapon){ Destroy(curWeapon); } curWeapon = Instantiate(weapons[weapon],transform.position,transform.rotation); } There's an interesting alternative to Destroy/Instantiate the weapons: activate the selected weapon and deactivate the others, like this:

var weapons: GameObject[]; private var curWeapon: int = 0; // now curWeapon is an int!

function ChangeWeapon(weapon:int){ curWeapon = weapon; for (var i = 0; i < weapons.length; i++){ weapons[i].SetActiveRecursively(i == weapon); } } In this case, you must drag the weapon prefabs to the scene, adjust their positions and drag them to the weapons array. You must also call ChangeWeapon(initial-weapon-number) at Start to select the first weapon (or no weapon at all, if you pass -1 to ChangeWeapon). The main advantage of this method is the freedom to adjust each weapon position independently of the others. Another advantage: weapon script variables like ammo count will not be lost each time you change weapons.

Comment
Add comment · Show 7 · 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 Bunny83 · Dec 11, 2011 at 07:10 PM 1
Share

I just want to add that it was ok for curWeapon to be a GameObject since it holds the current instance. Your example won't work since you try to Destroy the curWeapon which is now an int ^^

The error happens because he should have used the int parameter "weapon" of his ChangeWeapon function as index for the array.

But yes, it's also a good idea to hold the current index in case you want a next / previous button ;). Activating / deactivating is also the perfered method since Instantiate and Destroy are quite heavy functions.

avatar image aldonaletto · Dec 11, 2011 at 07:20 PM 0
Share

For God's sake! I messed things more than the original question! Thanks a lot, @Bunny83. I fixed the answer.

avatar image Dakwamine · Dec 11, 2011 at 07:26 PM 0
Share

In your first script, Destroy(curWeapon) will try to destroy the int variable. Not the weapon GameObject.

But your second script is O$$anonymous$$. ;)

avatar image aldonaletto · Dec 11, 2011 at 07:33 PM 0
Share

Thanks, it was a big rubbish (I missed the use of curWeapon in destroy/instantiate - shame on me!) - I've just fixed the answer.

avatar image xanderdood · Dec 11, 2011 at 08:58 PM 0
Share

is it possible to see a fully functional fixed version? this is still racking my brain!

Show more comments

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

6 People are following this question.

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

Related Questions

Passing a set of variables from one script to another 3 Answers

Multiple Melee Weapons 2 Answers

Bootcamp Weapon Hud & Reloading Problem 2 Answers

switch weapons script not working? 2 Answers

How do you use weapon handling? 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