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 icelated · Sep 16, 2012 at 05:26 AM · c#arrayweapons

Start() not firing when switching guns

I currently have a ak47 weapons class for shooting, animations, etc I also have a mk11 sniper class for the same reasons.

I have a weapons switching script that seems to be buggy. In my weapons switching script i have an array for the guns. I start with no weapon and i can select 2 on the keyboard and the ak47 shows up and start fires() in the ak47 class then i can select key 4 for the sniper and start fires(). This is where it gets interesting. When i then select 2 the start does not fire but the gun(ak47) is selected. I can then fire the gun and everything updates. when the ak47 is selected and i hit 4 the sniper is loaded but the start is not fired. I even added a print() in the start to see when its being fired.

My hierarchy: FPS first person controller. parented to that is a blank object "WeaponCameraRoot" parented to that is my weapons camera. Parented to that is a blank object called "PlayerWeapons" its children are my weapons.

I have the weapons select script attached to the blank object "PlayerWeapons" that the parent of my weapons.

Here is my weapons select script

 public class WeaponsSelect : MonoBehaviour {
     
     public GameObject[] weapons;
     public int selectedWeapon; 
 
     
     void Start () {
     
         SelectWeapon(0);
         selectedWeapon = 0;
         
     }
     
     
     void Update () {
         
     
         if(Input.GetKeyDown("1") && selectedWeapon != 1){
             if(selectedWeapon < 1)
             {
                 SelectWeapon(0);
                 selectedWeapon = 1;
             }
         }
         
         else if(Input.GetKeyDown("2")&& selectedWeapon != 2){
             SelectWeapon(1);
             selectedWeapon = 2;
         }
         
         else if(Input.GetKeyDown("3")&& selectedWeapon != 3){
             SelectWeapon(2);
             selectedWeapon = 3;
         }
         
         else if(Input.GetKeyDown("4")&& selectedWeapon != 4)
         {
             SelectWeapon(3);
             selectedWeapon = 4;
         }
     }
 
 
 
     private void SelectWeapon(int index)
     {
         foreach(GameObject obj in weapons)obj.SetActiveRecursively(false);
         weapons[index].gameObject.SetActiveRecursively(true);
     }
 }

I dont think theres a need to show you all the ak47 class.

     void Start () {
         
         print ("start called");
         anim = (AK47_animations)FindObjectOfType(typeof(AK47_animations));
         bulletsLeft = bulletsPerClip;
         audio.clip = load;
         audio.Play();
         
         Gui ();
         // MuzzleFlash.enabled = false;
          //muzzleLight.enabled = false;
     
     }


For some reason it will let me switch out the guns but the start() in each class only works once..

What can i do?

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

Answer by whydoidoit · Sep 16, 2012 at 10:55 AM

You are better off using OnEnable rather than Start if the item will be switched on and off. OnEnable will be called every time the item is activated (you can use OnDisable for deactivation too).

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 icelated · Sep 16, 2012 at 06:04 PM 0
Share

Thank you this works perfectly.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

Move files from folder a to folder b 1 Answer

Array index help. 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