Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by EleteKnyte · Jun 11, 2021 at 07:58 PM · errorarraysetactiveactive

object.activeSelf and object.activeInheirarchy Both not working

I'm currently writing a weapon switcher script for a learning project in unity, but when I run my code , it never gets past the activeSelf, or in the current code, activeInHeirarchy, if statement.

To give an example, on lines 125 - 135 (in case 1), the code would log "primary active", but never get to "hands deactivated". Heres my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponHandler : MonoBehaviour
 {
     public GameObject[] infantryRifle;
     public GameObject[] scoutRifle;
     public GameObject[] sidearm;
 
     public GameObject hands;
 
     public GameObject[] Crosshair;
 
     #region Weapons
     // Current equiped assault rifle | 0 = none | infantry rifle var 1 = 1 | infantry rifle var 2 = 2 | etc
     public float infantryPrimaryLoadoutIndex;
 
     // Current equiped sidearm | 0 = none | 1 = sidearm var 1 | 1 = sidearm var 2 | etc
     public float sidearmLoadoutIndex;
 
     // Current equiped sniper rifle | 0 = none | 1 = scout rifle var 1 | 2 = scout rifle var 2 | etc
     public float scoutPrimaryLoadoutIndex;
     #endregion
 
     #region Indexes
     //Weapon that is current active and usable | 0 = hands | 1 = primary | 2 = sidearm |
     public float currentWeaponIndex;
     //Class chosen in menu | 0 = infantry | 1 = scout | 
     public float currentClassIndex;
     #endregion
 
     void Start()
     {
         currentWeaponIndex = 1;
         currentClassIndex = 0;
 
         //Deactivate all weapons
         foreach (var obj in infantryRifle)
         {
             obj.SetActive(false);
         }
         foreach (var obj in scoutRifle)
         {
             obj.SetActive(false);
         }
         foreach (var obj in sidearm)
         {
             obj.SetActive(false);
         }
         foreach (var obj in Crosshair)
         {
             obj.SetActive(false);
         }
         hands.SetActive(false);
     }
 
     void Update()
     {
         //it does what it says
         getIndexInput();
 
         //Check what class is selected
         switch(((int)currentClassIndex))
         {
             //Load Correct Class
             //Class 0 aka Infantry
             case 0:
                 updateInfantry();
                 break;
         }
     }
     
     void getIndexInput()
     {
         //Get input for weapon index aka select a weapon
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             Debug.Log("Key down = 1");
             currentWeaponIndex = 1;
         }
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             Debug.Log("Key down = 2");
             currentWeaponIndex = 2;
         }
         if (Input.GetKeyDown(KeyCode.Alpha3))
         {
             Debug.Log("Key down = 3");
             currentWeaponIndex = 0;
         }
     }
 
     void updateInfantry()
     {
         int RLI = ((int)infantryPrimaryLoadoutIndex);
         int SLI = ((int) sidearmLoadoutIndex);
 
         GameObject rifle = infantryRifle[RLI];
         GameObject side = sidearm[SLI];
 
         switch (((int)currentWeaponIndex))
         {
             //If hands should be active and usable
             case 0:
                 Debug.Log("hands active");
                 //if primary or sidearm are active when the primary weapon should be, deactivate them
                 if (rifle.activeInHierarchy)
                 {
                     rifle.SetActive(false);
                 }
                 if (side.activeInHierarchy)
                 {
                     side.SetActive(false);
                 }
 
                 //if hands are not active, active them
                 if (!hands.activeInHierarchy)
                 {
                     hands.SetActive(true);
                 }
                 break;
 
             //If primary infantry weapon should be active and usable
             case 1:
                 Debug.Log("primary active");
                 //if sidearm or hands are active when the primary weapon should be, deactivate them
                 if (side.activeInHierarchy)
                 {
                     side.SetActive(false);
                 }
                 if (hands.activeInHierarchy)
                 {
                     hands.SetActive(false);
                     Debug.Log("hands deactivated");
                 }
 
                 //if currently equiped weapon is not active, activate it
                 if (!rifle.activeInHierarchy)
                 {
                     rifle.SetActive(true);
                 }
                 
                 break;
 
             //If sidearm should be active and usable
             case 2:
                 Debug.Log("sidearm active");
                 //if primary or hands are active when the primary weapon should be, deactivate them
                 if (rifle.activeInHierarchy)
                 {
                     rifle.SetActive(false);
                 }
                 if (hands.activeInHierarchy)
                 {
                     hands.SetActive(false);
                 }
 
                 //if currently equiped sidearm is not active, activate it
                 if (side != null)
                 {
                     if (!side.activeInHierarchy)
                     {
                         side.SetActive(true);
                     }
                 }
                 break;
         }
 
     }
 }
 
 
 
 

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

0 Replies

· Add your reply
  • Sort: 

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

174 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 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 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 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 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 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 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 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

Unity Inspector public large array - Lag and errors - Optimized GUI Block text buffer too large 0 Answers

Getting an error when using an Array of String Arrays 0 Answers

SetActive the first button that is not active in order of an array 0 Answers

Syncing SetActive over network 0 Answers

Index out of range error 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