- Home /
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;
}
}
200 lines of code is a lot to process... so try to post only relevant code....have you tried debugging the value of CurWeapon?
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.
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?
Yes my knife is actualy like a gun that shoots only a few cm away from me.Yes they are two weapons.
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
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.
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
Follow this Question
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