- Home /
[SOLVED] Cycling all the way through weapons properly
Thanks to the amazing ardizzle for solving this! To me, working through this post is EXACTLY what this community is all about!
Edit: You know, I realize that maybe I'm asking the wrong question, and making this far too complicated. Let me ask this:
Is there a way to call a function multiple times in the same script? Right now, if I try I get a "StackOverflow" error.
The gist of my post below is I have three weapons. (According to the script, four, but one of them is simply an empty hand) By pressing the "Weapon Switching" button on the gamepad the weapon switches to the next weapon, ironically enough. The issue is that when I get to the final weapon it stays on that rather than cycling back to the first one (Empty hand) and starting the process over. So my latest thought is if I could just call the original function again (SelectWeapon, or even Update) in the script below, that might work. I can't believe how incredibly frustrated I am by this, it seems like it would be such a simple thing to do, but this issue has dogged me for a month now. [END OF EDIT]
Hello again,
I've been having some success using a weapon switching script, but need to cycle completely through the player's weapons. Right now player is able to cycle from 1 - 4, but then stays at weapon 4. I need them to be able to restart at 1 once they have 4 equipped, and have pressed the "weapon switching" controller button. I tried calling "Update" again, only to get a stack overflow. (ok, that makes sense, but was willing to try it) Any help is greatly, and humbly, appreciated. God bless.
var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
var Weapon04 : GameObject;
static var weaponready = false;
static var swordready = false;
static var cameraready = false;
function Update () {
if (Input.GetButtonDown("Weapon Switch")) {
if (weaponready == true)
SelectWeapon();
}
}
function SelectWeapon () {
if (Weapon01.active == true)
{
if (WeaponInv.Club >=1)
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(true);
Weapon03.SetActiveRecursively(false);
Weapon04.SetActiveRecursively(false);
}
if (Input.GetButtonDown("Weapon Switch")) {
if (WeaponInv.Sword >=1)
if (swordready == true)
SwordSelect();
}
}
function SwordSelect () {
if (Weapon02.active == true)
{
if (WeaponInv.Sword >=1)
if (swordready == true)
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(true);
Weapon04.SetActiveRecursively(false);
}
if (Input.GetButtonDown("Weapon Switch")) {
if (cameraready == true)
CameraSelect();
}
}
function CameraSelect () {
if (Weapon03.active == true)
{
if (WeaponInv.Camera >=1)
if (cameraready == true)
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(false);
Weapon04.SetActiveRecursively(true);
}
if (Input.GetButtonDown("Weapon Switch")) {
Update();
}
}
darn it. Tried "invokerepeating" too, hoping that would work. Nein.
First of all y not have a list or array list or array for the weapons...
the array thing usually comes back to bite me in the butt, lol. The long and short of it is that I was having a heck of a time saving arrays to playerprefs. (and even playerprefsx)
Any reason you don't have an array of GameObjects and use and int as the index? Then you can just update switch to the next weapon with "wIndex = (wIndex+1)%4;" which will handle the looping back to 0. To set active you can use a for loop and deter$$anonymous$$e if the current weapon should be active using "wIndex == i".
Answer by ardizzle · Nov 14, 2013 at 03:58 AM
Ok man. I wrote you up a script that should work. You might have to make some small changes to make it work for your project. I just made 4 game objects and made it turn them on and off in an order. Here it is :
#pragma strict
var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
var Weapon04 : GameObject;
// Check to see if you have these weapons yet.
var obtainedWeapon03 : boolean = false;
var obtainedWeapon04 : boolean = false;
static var weaponready = true;
static var swordready = false;
static var cameraready = false;
static var equipSlot : int = 1;
function Start()
{
WeaponsFalse(); // Makes sure you don't have more than one weapon set.
Weapon01.SetActive(true);// Makes sure you start the game with your first weapon
}
function Update () {
if(Input.GetMouseButtonUp(0))// You will have to change to what you need
{
if(weaponready == true)
{
WeaponChange();
}
}
}
function WeaponChange()
{
if(Weapon01.activeSelf == true)
{
// Changes to the second weapon
WeaponsFalse();
Weapon02.SetActive(true);
Debug.Log ("Second Weapon");
}
else if(Weapon02.activeSelf == true)
{
// If you have the 3rd weapon it switches else goes back to 1st weapon
if(obtainedWeapon03 == true)
{
WeaponsFalse();
Weapon03.SetActive(true);
Debug.Log ("Third Weapon");
}
else
{
Debug.Log("Sorry you only have 2 weapons");
WeaponsFalse();
Weapon01.SetActive(true);
}
}
else if(Weapon03.activeSelf == true)
{
// If you have the 4rd weapon it switches else goes back to 1st weapon
if(obtainedWeapon04 == true)
{
WeaponsFalse();
Weapon04.SetActive(true);
Debug.Log ("Fourth Weapon");
}
else
{
Debug.Log("Sorry you only have 3 weapons");
WeaponsFalse();
Weapon01.SetActive(true);
}
}
else if(Weapon04.activeSelf == true)
{
// Goes back to the 1st weapon
WeaponsFalse();
Weapon01.SetActive(true);
Debug.Log ("First Weapon");
}
}
function WeaponsFalse()
{
// Deactivates all weapons to avoid having more than one weapon active at once
Weapon01.SetActive(false);
Weapon02.SetActive(false);
Weapon03.SetActive(false);
Weapon04.SetActive(false);
}
I also added some variables so that if you don't have the 3rd weapon or the 4th weapon it will set you back to the first weapon. What do you think?
had thought of that, but when I tried to implement it, nothing happened. Actually, worse than nothing, it blocked weapon 3. :(
Dang, wish I had saved that version ($$anonymous$$ono crashed) so I could show what I did.
ok then try something like this
function WeaponChange()
{
if(Weapon01.active == true)
{
WeaponsFalse();
Weapon02.active = true;
}
else if(Weapon02.active == true)
{
WeaponsFalse();
Weapon03.active = true;
}
else if(Weapon03.active == true)
{
WeaponsFalse();
Weapon04.active = true;
}
else if(Weapon04.active == true)
{
WeaponsFalse();
Weapon01.active = true;
}
}
function WeaponsFalse()
{
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(false);
Weapon04.SetActiveRecursively(false);
}
I think something along those lines should work and make your code shorter.
hey ardizzle, thanks for that. Still trying to utilize what you've got there with what I'm trying to do, but running into snags. Unfortunately, the way I have it set up right now doesn't allow me to change to ANY weapons. Still giving it the college try :) Posted below:
var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
var Weapon04 : GameObject;
static var weaponready = false;
static var swordready = false;
static var cameraready = false;
static var equipSlot : int = 1;
function Update () {
if (Input.GetButtonDown("Weapon Switch")) {
Debug.Log ("script firing");
if (weaponready == true)
WeaponChange();
}
}
function WeaponChange()
{
if(Weapon01.active == true)
{
WeaponsFalse();
Weapon02.active = true;
}
else if(Weapon02.active == true)
{
WeaponsFalse();
Weapon03.active = true;
}
else if(Weapon03.active == true)
{
WeaponsFalse();
Weapon04.active = true;
}
else if(Weapon04.active == true)
{
WeaponsFalse();
Weapon01.active = true;
}
}
function WeaponsFalse()
{
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(false);
Weapon04.SetActiveRecursively(true);
}
No problem. I had many people go above and beyond for me. Also if you ever need to hire an extra programer I do freelance work. You can email me at ardizzle@gmail.com
Your answer
Follow this Question
Related Questions
Trying to cycle through weapons 1 Answer
Cant call function in js another script 1 Answer
Cutscene Segments 1 Answer
Object will not instantiate 1 Answer
Unities Javascript, declaring and using a non-generic function 3 Answers