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 /
  • Help Room /
avatar image
1
Question by Ragatto · Jan 25, 2017 at 01:14 PM · gameobjectarrayfpsinventoryshooter

Switching the index of a GameObject Array type?

Hi, I made more than a weapon in Unity, but now I need something to switch the

public GameObject[] inventory;

But later, the compiler saied I can't use integer to switch the index of a gameObject Array.

I need to set active by integer the gameObject stored in a slot (1,2,3 etc).

Moreover the weapon should be changable by holding E, and they are:

1 = Melee, 2 = Pistols, 3 = Shotguns etc Like a fps arena... but the question is just for the gameObject array use.

Some help? Thanks! :)

Comment
Add comment · Show 2
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 Jacky-Marley · Jan 25, 2017 at 03:38 PM 1
Share

can you send us the part of code you've writted about this function ?

avatar image Ragatto Jacky-Marley · Jan 26, 2017 at 02:56 PM 0
Share

I responded down(It's strange that the tag @Jacky-$$anonymous$$arley didn't find you, so I erased it).

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jacky-Marley · Jan 27, 2017 at 10:30 AM

  void ChangeWeapon()
     {
         foreach ( GameObject weapon in inInventory)
         {
             weapon.setActive(false);
         }
         inInventory[indexOfTheWeapon].setActive(true);
     }

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 Ragatto · Jan 27, 2017 at 05:13 PM 0
Share

It seems to be ok as code, but now there are problems with the integration.

     void ChangeWeapon()
     {
         foreach (GameObject activeWeapon in inInventory)
         {
             activeWeapon.SetActive(false);
         }
         inInventory[(int)INVENTORY_$$anonymous$$axSlot.Slot1].SetActive(true);
     }

What happens is each slot selects the same type of weapon. Anyway I'll try to debug, thanks! :)

avatar image
0

Answer by Ragatto · Jan 26, 2017 at 02:53 PM

During this time I modified the code: I used enums, so now it works! The only problem I don't know how active the wanted slot, deactivating the other ones.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponSelection : MonoBehaviour {
 
     public GameObject[] inInventory = new GameObject[(int)INVENTORY_Melee.SlotCount];
     private GameObject activeWeapon;
 
     public enum INVENTORY_Melee
     {   
         Empty,
         Hammer,
         SlotCount
     }
     public enum INVENTORY_Guns
     {   
         Empty,
         NailGun,
         RevolverM_500,
         SlotCount
     }
     public enum INVENTORY_ShotGuns
     {
         Empty,
         SlotCount
     }
     public enum INVENTORY_MachineGuns
     {
         Empty,
         SlotCount
     }
     public enum INVENTORY_Explosives
     {
         Empty,
         SlotCount
     }
     public enum INVENTORY_Rifles
     {
         Empty,
         SlotCount
     }
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
     if (Input.anyKeyDown) {
             switch (Input.inputString)
             {
                 case "0":
                     break;
 
                 case "1":
                     GameObject activeMelee = inInventory[(int)INVENTORY_Melee.Hammer - 1];
                     activeWeapon = activeMelee;
                     break;
 
                 case "2":
                     GameObject activeGun = inInventory[(int)INVENTORY_Guns.NailGun];
                     activeWeapon = activeGun;
                     break;
 
                 case "3":
                     GameObject activeShotguns = inInventory[(int)INVENTORY_ShotGuns.Empty];
                     break;
 
                 case "4":
                     GameObject activeMachineGuns = inInventory[(int)INVENTORY_MachineGuns.Empty];
                     break;
 
                 case "5":
                     GameObject activeExplosives = inInventory[(int)INVENTORY_Explosives.Empty];
                     break;
 
                 case "6":
                     GameObject activeRifles = inInventory[(int)INVENTORY_Rifles.Empty];
                     break;
 
                 default:
                     break;
             }
         }
     }
 }
 

I tried using gameObject.setActive(bool), but (again, )I need something which I can set active the gameobject in the slot I selected, setting not-active the other ones.

Just this for now.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A few tips on FPS design. 0 Answers

How to make 5 GameObjects swap between 5 predetermined positions (1 gameobject occupy 1 position per time)? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Reference GameObjects from an Array 1 Answer

Multiple Instantiations not working 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