Boolean gets true no matter what

I have a script which handles the weaponswitching of my game.
strangely enough one of the variables is true no matter what, and i can’t find why. Could anybody please tell me why it does this? Because it now applies the damage even if other weapons are active…

#pragma strict

var Weapon1 : GameObject;
var Weapon2 : GameObject;
var RightHand : boolean = false;
Weapon2.active=false;
RightHand = false;

//var MaceDamage = 75;
var RiHaDamage = 20;
//var SwordDamage = 85;
var Distance : float;
var MaxDistance : float = 1.5;


function Update(){

if(Input.GetKeyDown(KeyCode.Q))
	{
    SwapWeaponsR();
	}    
	

if (RightHand==true);
	{
	Debug.Log("RHTRUE");
	if (Input.GetButtonDown ("Fire1"))
	{
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
		{
			Distance = hit.distance;
			if (Distance < MaxDistance)
			{
					hit.transform.SendMessage("ApplyDamage", RiHaDamage, SendMessageOptions.DontRequireReceiver);
			}
		}
	
	}

}
	

	
	
	
}    

function SwapWeaponsR() {
 
    if(Weapon1.active == true){
       Weapon1.SetActive(false); 
       Weapon2.SetActive(true);
    }
    else if(Weapon2.active == true){
       Weapon2.SetActive(false); 
       RightHand = true;
    }
    else if(RightHand == true){
       RightHand = false; 
       Weapon1.SetActive(true);
    }
}

P.S. Those two lines which i commented out, are for later.

Hi Hindrik,

At a first glance, Line 6 of your code

Weapon2.active=false;

Should be

Weapon2.SetActive(false);

I could be wrong, but I have had similar issues in the past, where this solved the issue.

This way to switch weapons seems unreliable. You could use another approach: increment a variable curWeapon in a circular fashion (0,1,2,1,2,0 etc.) and use it to activate only the current weapon and deactivate all others. The code below does this, and also improves Raycast a little:

#pragma strict
 
var Weapon1 : GameObject;
var Weapon2 : GameObject;
var RightHand : boolean = false;
var curWeapon : int = 1; // start with weapon 1
 
//var MaceDamage = 75;
var RiHaDamage = 20;
//var SwordDamage = 85;
var Distance : float;
var MaxDistance : float = 1.5;
 
function Start(){
  SelWeapon(); // select the initial weapon
}

function SelWeapon(){
  // activate only curWeapon, deactivate the others:
  RightHand = (curWeapon == 0);
  Weapon1.SetActive(curWeapon == 1);
  Weapon2.SetActive(curWeapon == 2); 
}

function Update(){
  if (Input.GetKeyDown(KeyCode.Q))
  {
    // increment curWeapon in a circular fashion:
    curWeapon = (curWeapon + 1)% 3;
    // select curWeapon:
    SelWeapon();
  }    
  if (RightHand);
  {
    Debug.Log("RHTRUE");
    if (Input.GetButtonDown ("Fire1"))
    {
      var hit : RaycastHit;
      // do a raycast limited to MaxDistance range:
      if (Physics.Raycast (transform.position, transform.forward, hit, MaxDistance)) 
      {
        hit.transform.SendMessage("ApplyDamage", RiHaDamage, SendMessageOptions.DontRequireReceiver);
      }
    }
  }
}

This stray semicolon is causing your problem:

if (RightHand==true);//this semicolon should be removed
    {
    Debug.Log("RHTRUE");
    if (Input.GetButtonDown ("Fire1"))
    {
       var hit : RaycastHit;
       if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
       {
         Distance = hit.distance;
         if (Distance < MaxDistance)
         {
              hit.transform.SendMessage("ApplyDamage", RiHaDamage, SendMessageOptions.DontRequireReceiver);
         }
       }
    }
}

It is causing your code to be interpreted as:

if (condition) /* some empty block of code*/;
{
    // some other unrelated block of code that will execute
    // regardless of the condition evaluated in the if statement
}