- Home /
Changing weapons script
I working on a FPS game, and i have a problem to change weapons :s
here is the script:
public string CurrentWeapon;
public GameObject M9;
public GameObject G36C;
void Start() {
CurrentWeapon = "M9";
}
public void ChangeWeapon() {
if(Input.GetKey(KeyCode.Q)) {
CurrentWeapon = "G36C";
}
if(Input.GetKey(KeyCode.E)) {
CurrentWeapon = "M9";
}
if(CurrentWeapon == "G36C") {
M9.active = false;
G36C.active = true;
}
if(CurrentWeapon == "M9") {
M9.active = true;
G36C.active = false;
}
}
I have attached the script on FPS controller, then i link the weapons to the script. but still dont change weapons :s
P.S = M9 and G36C are weapons.
Answer by KMKxJOEY1 · Mar 28, 2013 at 03:06 AM
Your not calling the function
Add this:
Void Update()
{
ChangeWeapon();
}
Answer by Unity_scat · Sep 27, 2015 at 05:01 PM
I made a Javascript script on this. Create two tags called Pistol and Stick, a terrain, an empty game object, two prefabs, attach the Pistol and Stick tags to the prefabs, attach the code to the empty game object, attach the prefabs to the Pistol and Stick prefab slots, and done!
//Variables
var Pistol : GameObject;
var Stick : GameObject;
var pistolisspawned = true;
var stickisspawned = false;
var canswitchtopistol = false;
var canswitchtostick = true;
function Start () {
ToPistol();
}
function Update () {
if(pistolisspawned == true) {
canswitchtopistol = false;
canswitchtostick = true;
}
if(stickisspawned == true){
canswitchtostick = false;
canswitchtopistol = true;
}
WeaponSwitch();
}
function WeaponSwitch() {
if (Input.GetKeyDown("q")) {
ToPistol();
}
if (Input.GetKeyDown("e")) {
ToStick();
}
}
function ToPistol () {
if(canswitchtopistol == true) {
Destroy (GameObject.FindWithTag("Stick"));
var clone : GameObject;
clone = Instantiate(Pistol, transform.position, transform.rotation);
pistolisspawned = true;
stickisspawned = false;
}
}
function ToStick () {
if(canswitchtostick == true) {
Destroy (GameObject.FindWithTag("Pistol"));
var clone : GameObject;
clone = Instantiate(Stick, transform.position, transform.rotation);
stickisspawned = true;
pistolisspawned = false;
}
}
Answer by MorganLee909 · Sep 27, 2015 at 05:02 PM
Also, it would make for shorter, simpler code if you just set the weapons active/inactive with the input. Then you would not even need the currentWeapon variable unless you need it elsewhere in your code. Either way, you wouldn't need those last two if statements.
Answer by PVisser · Aug 27, 2017 at 05:01 AM
Here's my version of this script in C# with some slight adjustments. You're activating and deactivating objects so you'll need a third object as a holder for the script since a deactivated object can't re activate itself. Have the one object you want to start with turned on and the others turned off in the editor.
public class WeaponChange : MonoBehaviour {
public string CurrentWeapon; //looks for the name of weapons
public GameObject M9; //Add objects "inventory" style
public GameObject G36C;
void Start()
{
CurrentWeapon = "M9"; //what you're holding
}
void Update()
{
ChangeWeapon(); //checks for weapon change input
}
public void ChangeWeapon()
{
if (Input.GetKey(KeyCode.Q))
//On Input turn this one ON and others OFF
{
CurrentWeapon = "G36C";
G36C.SetActive (true);
M9.SetActive (false);
}
if (Input.GetKey(KeyCode.E))
//On Input turn this one ON and others OFF
{
CurrentWeapon = "M9";
M9.SetActive(true);
G36C.SetActive(false);
//You can also add code for sound, particles, etc.
}
}
}
Your answer
Follow this Question
Related Questions
Player moves faster when fps is higher? 1 Answer
How do I put a delay on a gunshot? 2 Answers
Weapon Possition Move On Sprint 1 Answer
Where is the FPSPlayer Script? 0 Answers