Search Unity

SetActive() problem

Discussion in 'Scripting' started by PenguinLord, Nov 22, 2016.

  1. PenguinLord

    PenguinLord

    Joined:
    Sep 27, 2013
    Posts:
    9
    So i'm using setactive for weapon switching for a basic game.

    But i have a weapon script which deals with damage and fire rate
    when i switch it does not disable the last script on the weapon therefor the scripts merge in the weapons and they both have the same fire rate?
     
  2. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Code (CSharp):
    1. public GameObject[] weapon;
    2.  
    3. void Start(){
    4. for (int i = 0; i < weapon.Length; i++){
    5. weapon.SetActive(false);
    6. }
    7. weapon[currentWeapon].setActive (true);
    8. }
    If you use something like that where currentWeapon is just an int that references the current weapon you want equipped.
     
  3. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Also are you adding a weapons class to each weapon and modifying the values, make a global weapon class that you can add to each weapon and then set each variable, such as damage and fire rate. Make them public and you can just set these in the inspector and customise at will.