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 Albert-han · Jul 13, 2014 at 06:30 AM · errornotunexpected

Switching between two weapons

Hi guys i am currently using two scripts to choose my weapon before I spawn my player into the game and use two models.(knife and gun)But when i choose my gun, my (knife model) spawns too.Same the other way too.I have no idea why this happens.Here's my script.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WeaponManager : MonoBehaviour {
     public List<Gun> Weapons = new List<Gun>();
     public int CurWeapon;
     public static WeaponManager Instance;
     // Use this for initialization
     void Start () {
         Instance = this;
     
     }
     
     // Update is called once per frame
     void Update () {
         CurWeapon = GUIManager.Instance.CurWeapon;
 
     }
 
     public void Spawn()
     {
         CurWeapon = GUIManager.Instance.CurWeapon;
         transform.root.GetComponent<Character> ().Server_GetGun (Weapons [CurWeapon].Name);
         ApplyWeapon();
 
 
     }
 
     public void ApplyWeapon()
     {
         foreach (Gun gu in Weapons)
         {
             if(gu == Weapons[CurWeapon])
             {
                 gu.gameObject.SetActive(true);
             }
             else
             {
                 gu.gameObject.SetActive(false);
             }
         }
     }
 
     public static Gun FindWeapon(string Name)
     {
         foreach (Gun Gu in Instance.Weapons)
         {
             if(Name == Gu.Name)
                 return Gu;
         }
         return null;
     }
 }

The other one:(near Client_GetGun)

     [RPC]
     public void Client_GetGun(string name)
     {
         foreach (ThirdPersonGuns tpg in Guns) 
         {
             if(tpg.Name == name)
             {
                 tpg.Obj.SetActive(true);
                 Debug.Log (tpg + "Done");
             }
             else
             {
                 tpg.Obj.SetActive (false);
                 Debug.Log (tpg + "Not Done");
         }
     }
 }
 
 [System.Serializable]
 public class ThirdPersonGuns
 {
     public string Name;
     public GameObject Obj;
 }
 }


Comment
Add comment · Show 6
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 Fornoreason1000 · Jul 13, 2014 at 07:00 AM 0
Share

200 lines of code is a lot to process... so try to post only relevant code....have you tried debugging the value of CurWeapon?

avatar image Albert-han · Jul 13, 2014 at 07:10 AM 0
Share

Updated.Sorry dint realise it was that long.The console doesnt give an error but when i pick (Gun) my knife model doesnt disable itself.

avatar image tanoshimi · Jul 13, 2014 at 07:27 PM 0
Share

I'm assu$$anonymous$$g that Gun and $$anonymous$$nife are the two items in the Weapons list? (i.e. ypour knife is actually a Gun?) Can you post your Gun class?

avatar image Albert-han · Jul 14, 2014 at 02:21 AM 0
Share

Yes my knife is actualy like a gun that shoots only a few cm away from me.Yes they are two weapons.

avatar image Albert-han · Jul 14, 2014 at 02:41 AM 0
Share

Here's all my scripts associated with the gun.I know it is quite long but there might be some important piece on the top too.Sorry http://pastebin.com/cTAQrVAg - GUI$$anonymous$$anager http://pastebin.com/DXQ0JwcX - Weapon $$anonymous$$anager http://pastebin.com/XwaCa0gm - Gun

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Jul 14, 2014 at 03:10 AM

I don't see anything immediately wrong with the code above, but you could approach this a slightly more efficient way (and who knows, it might fix it). Instead of looping through all weapons and turning 1 on and the rest off (I assume that's what you're doing). Just track the current weapon and previous weapon. Then when you set the current weapon, turn the previous weapon off if it is not null. Then set the previous to the current for next time.

If that still doesn't work, how is your game object setup? Do you have a single prefab object, or a parent object with children, and is that parent (the prefab root) object the one that contains your Gun script? If the Gun script is on a child of the prefab parent, then you are only setting the gameObject to inactive for that child object (the one the Gun script is on), and presumably the renderer is on a different gameObject in the hierarchy.

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 Albert-han · Jul 14, 2014 at 03:24 AM 0
Share

i have two gun models both with gun script.They are child of my first person controller.I'm trying your setup now.Thanks

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

24 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

Related Questions

Cant Receive Exp 1 Answer

WeaponScript not working 0 Answers

Error unexpected symbol 'internal' What is wrong with this? 0 Answers

Not getting Score 0 Answers

Javascript literal error: unexpected identifier 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