Trying to switch weapons

Hi guys,

I am stuck with a problem its as follows:

I am trying to change my weapon which are of two types both are thrown when the mouse-button is UP. Now Iam using GUI buttons to switch the weapons by changing the state, but as soon as I click the GUI buttons the weapon gets instantiated and it gets thrown but that is because before instantiating I am checking if what is the state which gets changed as soon as I press the GUI Button.

Here is my Code.

//Variable Decleration
var gameManager : Game_Manager;
var obj1 : Rigidbody;
var obj2 : Rigidbody;
var playerHand : Transform;
private var mySpeed : int = 50;
private var reqdAnimTime : float = 0.67;
private var incrementTime : float = 0.0;
private var canCheckTime : boolean = false;
private static var onInGame : boolean = false;

private enum weaponObjects {NONE , WEAPON1 , WEAPON2}
private var selectedWeapon : int = weaponObjects.WEAPON1;


//Function Decleration
// Update function Decleration
function Update ()
{
	if(onInGame)
	{
		Throw_Object();
	}
}

// Start_Motion function Decleration
/*This Functions checks if the player is inside the game or not
 accordingly execuates the proper function*/
function On_In_Game() {
	print("I have received the message to Start the motion of the Player");
	onInGame = true;
}

/* Throw_Object function Decleration
This function is used to Instantiate and shoot the prefabs in the desired direction*/
function Throw_Object()
{
	var obj1Clone : Rigidbody;
	var myDirection : Vector3 = Input.mousePosition;
	
	if( (incrementTime >= reqdAnimTime)  (canCheckTime))
	{	
		canCheckTime = false;
		incrementTime = 0.0;
		if(selectedWeapon == weaponObjects.WEAPON1)
		{
			print("Weapon 1 is selected  will be instantiated");
			//This code instantiates the obj1 weapon
			obj1Clone = Instantiate(obj1, playerHand.position, playerHand.rotation);
			obj1Clone.transform.rotation = Quaternion.identity;
			obj1Clone.transform.LookAt(myDirection);
			obj1Clone.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * mySpeed;		
			Destroy(obj1Clone.gameObject,5);
		}

		if(selectedWeapon == weaponObjects.WEAPON2)
		{
			print("Weapon 2 is selected  will be instantiated");
			//This code instantiates the obj2 weapon
			obj2Clone = Instantiate(obj2, playerHand.position, playerHand.rotation);
			obj2Clone.transform.rotation = Quaternion.identity;
			obj2Clone.transform.LookAt(myDirection);
			obj2Clone.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * mySpeed;		
			Destroy(obj2Clone.gameObject,5);
		}
	}
	else
	{
		if(canCheckTime)
		{
			incrementTime+=Time.deltaTime;
		}
	}
	
	if(Input.GetButtonUp ("Fire1"))
	{
		animation.Play("idle");
		canCheckTime = true;
	}
}

/* Select_Weapon1 function Decleration
This function selects weapon number 1*/
function Select_Weapon1() {
	selectedWeapon = weaponObjects.WEAPON1;
}

/* Select_Weapon1 function Decleration
This function selects weapon number 2*/
function Select_Weapon2() {
	selectedWeapon = weaponObjects.WEAPON2;
}

where is your GUI Button man?

Hi

My GUI buttons are being created from another script in which on clicking the button it changes the status of the weapon.

raviraj what i think, the problem is in this script Because you declare the (private var selectedWeapon : int = weaponObjects.WEAPON1) directly in this script so it is not changing the sate of your weapon, That you are trying to change in your GUI script.

Hi sushanta

Thanks for the reply I think it could be the problem I will check it out.

Thanks