INT switching accordingly to other INT

so i want my weapons (game objects to switch ) acording to current weapon i have
so my current script is this

public WeaponObject [] Weapons ; /* this is the scriptable object i use for counting damge so i want to have my guns Game object mesh to change acordingly to this lets say if weapons[currentweapon] =1 guns is also = 1 and that they would actualy change */
    
  public int currentweapon = 0; // int for current weapon
  public GameObject [] guns; // aray of Gameobjects
  public Transform gunend;  // were the prefab is Instantiate
  
  /*other part of code is just
  if (press lalalal )
  { Fire();
  }
  */
  
  
     void Fire()
     {
       Timer = 0f;
       Rigidbody bullet;
       bullet = Instantiate (bulletprefab, gunend.position, gunend.rotation) as Rigidbody;
       bullet.AddForce (gunend.forward * 4000);
     }
  
  }

so is it posible to have them switch acordingly ?

I assume each weapon will switch to another specific weapon when you press the weapon switch key/button. Do you want them to just cycle through like gun->rifle->machinegun->rpg->gun ? or do you want certain one to always switch to certain ones like gun->rifle rifle->gun machine->rpg rpg->gun and if you needed to pick a specific one there would be some other menu to actually select from a list?

Well at first you have a currentweapon 0 i made a shop a button tipe UI canvas that alows you purchase weapons in game the weapon you buy switches the number on weapons[currentweapon] curently from 0 to 3 in any order so i need the gameobject [ ] guns to match the current weapon and switch the active gameobject gun

Make currentWrapon private. Then make a public method SwitchWeapon(int weapon).

That way you can update currentWrapon and the GameObject at the same time.

I cant make currentweapon private because of weapons shop it cant acces it if its private

Use this then.

public int currentweapon { get; private set; }
1 Like

Sure it can. Expose a public member that provides WeaponShop the details it needs.

Or you can look into properties, which are exactly the same thing.

The idea is that whenever currentWeapon changes, it should update some other things. And methods or properties are the way to do that.

1 Like

Can i just use something like

Gameobject guns = weapons[currentweapon];