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 Morokiane · Mar 23, 2015 at 05:17 PM · gameobjectinventoryweapon

Switching GameObject

I'm trying to make it so when a player collides with an icon for a weapon that weapon is activated on the player. I have an array that holds the game objects that are the actual weapons and the icons and everything seems to activate except for getting the game objects to move to the currentWeapon. The debug logs are giving me all the information I need, just don't know how to get the weapon working on the player. The error I get from Unity is, "Assets/Scripts/Player/SecondWeapon.cs(104,77): error CS0039: Cannot convert type SecondWeapon.WeaponPresetType' to UnityEngine.GameObject' via a built-in conversion`" which is "GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;"

SecondWeapon.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SecondWeapon : MonoBehaviour {
 
     public GameObject[] weapons; //array of secondary weapons
     public GameObject currentWeapon; //current secondary weapon
     public int weaponFireRate;
 
     public Transform gunPoint;
         
     float nextFire;
 
     // Holds the weapons in our inventory
     private bool[] weaponInventory;
     // Which weapon is the player currently using?
     private int currentWeaponIndex = 0;
 
     public enum WeaponPresetType {
         
         Empty,
         Missle,
         Boomerang,
     }
     
     private WeaponPresetType _weaponPreset;
     
     public delegate void WeaponPresetChangeHandler();
     
     public event WeaponPresetChangeHandler WeaponPresetChanged;
     
     public WeaponPresetType weaponPreset {
         
         get {
             return _weaponPreset;
         }
         set {
             if (weaponPreset == value) return;
             
             gunPoint.transform.localPosition = new Vector2 (0.6f, 0f);
             
             _weaponPreset = value;
             if (WeaponPresetChanged != null) WeaponPresetChanged();
         }
     }
     
     void Start() {
         
         WeaponPresetChanged += WeaponPresetChangedHandler;
 
         weaponInventory = new bool[4];
         weaponInventory[(int)WeaponPresetType.Empty] = true; // Only set first weapon to true (put it in the inventory)
     }
     
     private void WeaponPresetChangedHandler() {
 
         switch (weaponPreset) {
             
         case WeaponPresetType.Empty:
             weaponInventory[(int)WeaponPresetType.Empty] = true;
             break;
         
         case WeaponPresetType.Missle:
             weaponInventory[(int)WeaponPresetType.Missle] = true;
             PickupWeapon(1);
             Debug.Log ("missle");
             break;
         
         case WeaponPresetType.Boomerang:
             //weaponInventory[(int)WeaponPresetType.Boomerang] = true;
             PickupWeapon(2);
             Debug.Log ("boomerang");
             break;
 
         default:
             weaponInventory[(int)WeaponPresetType.Empty] = true;
             break;
         }
     }
     
     void Update() {
         
         //sets B button as secondary fire
         if (Input.GetButton ("Fire2") && Time.time > nextFire) {
             nextFire = Time.time + weaponFireRate;
             Instantiate (currentWeapon, transform.position, transform.rotation) as GameObject;
         }
     }
 
     public void PickupWeapon(int weapon)
     {
         weaponInventory[weapon] = true;
         SwitchToWeapon(weapon);
         currentWeaponIndex = weapon;
         print("Current weapon pick up is: " + currentWeaponIndex);
     }
 
     public void SwitchToWeapon(int weapon)
     {
         if (weaponInventory[weapon])
         {
             // animate putting currentWeapon away
             GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;
 
 
             Debug.Log ("switched weapon is " + weapon);
             Debug.Log (weaponInventory[weapon]);
             // animate pulling out currentWeapon
         }
     }
 }


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
2
Best Answer

Answer by spiceboy9994 · Mar 23, 2015 at 11:11 PM

From what I understood in your script ... weapon is an int... so :

 (WeaponPresetType)weapon

this is valid, and the result would be an enumeration based on your int index... but:

 GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;

This is not valid since, unity does not understand an enumeration as game object. Maybe you may need to refer to your weapon array of objects?

 GameObject currentWeapon = weapons[weapon]; // since this is game object

Be awared that you already have a currentWeapon as global variable, so this local variable may be ghosting the global one, not sure if that's something you want. Another option may be, keep your weapon and your equipped status within the same class something like:

 public class WeaponObject {
 
 public GameObject MyGameObject;
 public bool IsEquipped;
 
 }

That way you could keep the status along with the object, without worrying to synchronize 2 different arrays.

 var WeaponObject weapon = new WeaponObject ();
 var x = weapon.MyWeaponObject; // the weapon game object
 var y = weapon.IsEquipped; // the equipped status for that specific weapon

Hope this helps you a bit.

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

22 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

Related Questions

How would i check something in an Element in an array? 1 Answer

Serialized class, Instances and access 1 Answer

Array Help GameObject Length Inventory 1 Answer

Pick all gameobject with specific tag 1 Answer

How to load an item into the player's hand by clicking on a gameObject. 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