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 ExpiredIndexCard · Jul 08, 2013 at 12:48 AM · gunswitchboolactive

Gun Switch and SetActive is not Working!

I am making a 2.5D game and I am trying to make gun switching but the problem is that it doesn't work. There is no gun and there are no errors. What is wrong? (btw the array size is 3)

 using UnityEngine;
 using System.Collections;
 
 public class InventorySelect : MonoBehaviour {
     
     public  GameObject[] inventoryOfGuns;
     public     GameObject      player;
     public  Transform     gunPos;
     
     private int             currentPosInArray;
     
     
     void Awake () 
     {
         currentPosInArray = 0;
         
         for(int i = 0; i < 3; i++)
         {
             GameObject gunInstance = (GameObject)Instantiate(inventoryOfGuns[i], gunPos.position, Quaternion.identity);
             gunInstance.transform.parent = player.transform;
             inventoryOfGuns[i].SetActive(false);
         }
         inventoryOfGuns[0].SetActive(true);
         
     }
     
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             inventoryOfGuns[currentPosInArray].SetActive(false);
             if(currentPosInArray == 2)
             {
                 currentPosInArray = 0;
             }
             else
             {
                 currentPosInArray++;
             }
             Debug.Log(currentPosInArray);
             inventoryOfGuns[currentPosInArray].SetActive(true);
         }
         
         if(Input.GetKeyDown(KeyCode.Q))
         {
             inventoryOfGuns[currentPosInArray].SetActive(false);
             if(currentPosInArray == 0)
             {
                 currentPosInArray = 2;
             }
             else
             {
                 currentPosInArray--;
             }
             Debug.Log(currentPosInArray);
             inventoryOfGuns[currentPosInArray].SetActive(true);
         }
         
     }
 }
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 fafase · Jul 08, 2013 at 01:20 AM 0
Share

I'm confused, you have the inventory array that seem to be prefab since you create object of them in the awake.

Then you attach the instance to the player and deactivate the prefab. Later on you manipulate the prefabs in the array but never the instances you created. Is that fully intended?

avatar image ExpiredIndexCard · Jul 08, 2013 at 01:27 AM 0
Share

What do you mean?

avatar image fafase · Jul 08, 2013 at 01:32 AM 0
Share
 void Awake () {
    currentPosInArray = 0;
    for(int i = 0; i < 3; i++){
          GameObject gunInstance = (GameObject)Instantiate(inventoryOfGuns[i], gunPos.position, Quaternion.identity);
          gunInstance.transform.parent = player.transform;
          inventoryOfGuns[i].SetActive(false);
    }
    inventoryOfGuns[0].SetActive(true);
 }

this is your awake, you create three objects of the type of the inventory in gunInstance but you do not do anything with those. They are created and not use nor destroyed. Well, you attach them bu that is it. On the other hand, you have your inventory that you are activating and not. I would guess this inventory has the things you need but as they are not attached to the player, they just sit somewhere in the scene.

avatar image ExpiredIndexCard · Jul 08, 2013 at 01:48 AM 0
Share

How do I fix this?

2 Replies

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

Answer by fafase · Jul 08, 2013 at 02:23 AM

Two possibilities, the objects populating your inventory are either prefabs(A) or actual scene objects(B).

 using UnityEngine;
 using System.Collections;
  
 public class InventorySelect : MonoBehaviour {
  
     public  GameObject[] inventoryOfGuns;
     GameObject [] weapons = new GameObject[3];
     public     GameObject      player;
     public  Transform   gunPos;
     private int          currentPosInArray;
 
     void Awake () {
        currentPosInArray = 0;
        for(int i = 0; i < 3; i++)
        {
          weapons[i] = (GameObject)Instantiate(inventoryOfGuns[i], gunPos.position, Quaternion.identity);
          weapons[i].transform.parent = player.transform;
          weapons[i].SetActive(false);
        }
        weapons[0].SetActive(true);
     }
  
     void Update () 
     {
        if(Input.GetKeyDown(KeyCode.E))
        {
          weapons[currentPosInArray].SetActive(false);
          if(currentPosInArray == 2)
          {
           currentPosInArray = 0;
          }
          else
          {
           currentPosInArray++;
          }
          Debug.Log(currentPosInArray);
          weapons[currentPosInArray].SetActive(true);
        }
  
        if(Input.GetKeyDown(KeyCode.Q))
        {
          weapons[currentPosInArray].SetActive(false);
          if(currentPosInArray == 0)
          {
           currentPosInArray = 2;
          }
          else
          {
           currentPosInArray--;
          }
          Debug.Log(currentPosInArray);
          weapons[currentPosInArray].SetActive(true);
        }   
     }
 }

B version:

     void Awake () 
     {
        currentPosInArray = 0;
  
        for(int i = 0; i < 3; i++)
        {
          inventoryOfGuns[i].transform.position = gunPos.position;
          inventoryOfGuns[i].transform = player.transform;
          inventoryOfGuns[i].SetActive(false);
        }
        inventoryOfGuns[0].SetActive(true);
     }

The rest remains the same as your example

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 ExpiredIndexCard · Jul 08, 2013 at 05:18 AM 0
Share

Interesting let me test this code out. $$anonymous$$now I know what you mean by prefab instance. Thanks

avatar image
1

Answer by xortrox · Jul 08, 2013 at 01:57 AM

In this attachment I've sent 2 classes that should be sufficient for you to get the grasp of how this can be done, the code is untested (just wrapped it up in notepad right now) I've used this kind of technique before however.

Classes.zip


classes.zip (1.3 kB)
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 xortrox · Jul 08, 2013 at 02:03 AM 0
Share

forgot to add that the currently spawned weapon will be accessible from the Player class as weapon.weapon$$anonymous$$odel.

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

15 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

Related Questions

switch weapons script not working? 2 Answers

Hello I have a gun swich script that doesn't work can someone help me please? 2 Answers

On/Off switch for active objects...any ideas? 2 Answers

How to disable a bool switch under certain circumstances? 1 Answer

switching the active boolean 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