Call 2 Functions At The Same Time

Hi!
I Have a UMP And A M21 In My Game
I’m Trying To Make A Simple Weapon PickUp Script For My FPS Game , For The Moment There Are Only 2 Weapons To Pick up And I Stopped For Testing ,All Works Fine , But When i Pick up The M21 That Uses The Second Pick Up Function Of The Code It Pick Up The UMP And Throws The M21 , I Tried Inverting The Weapons Codes , I Pickup M21 And Throw Down The UMP , It Can Work Only If I Keep Pressing E. So I Think i Should Call Them Both At The Same Time.
Here Is The Code :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PickUpWeapon : MonoBehaviour {
    //Text To Ask The User If He Wants To Pickup The Weapon
    public GUIText PickUpText;
    //Weapon In Player Hands Variables
    public GameObject M21Model; 
    public GameObject UMPModel;
    public GameObject MP7Model;
    //Array Of The Guns We Can Pickup
    public GameObject[] M21PickUp;
    public GameObject[] UMPPickUp;
    public GameObject[] MP7PickUp;
    //The Weapon We Throw When We Get Another One
    public Rigidbody M21PickUpThrow;
    public Rigidbody UMPPickUpThrow;
    public Rigidbody MP7PickUpThrow;
    //The Gun We Should Throw
    private Rigidbody GunToThrow;
    //Current Weapon We Are Holding
    public int WeaponHoldIndex;
    //Distance To PickUp The Weapon
    public int distToPickUp;
  


	// Use this for initialization
	void Start () {
        //Config The Current Weapon
        if (M21Model.activeSelf)
            WeaponHoldIndex = 0;
        if (UMPModel.activeSelf)
            WeaponHoldIndex = 1;
        if (MP7Model.activeSelf)
            WeaponHoldIndex = 2;
        //Desactivate PickUp Text
        PickUpText.gameObject.SetActive(false);
        //Collect Pickup-able Weapons GameObjects
        M21PickUp = GameObject.FindGameObjectsWithTag("M21PickUp");
        UMPPickUp = GameObject.FindGameObjectsWithTag("UMPPickUp");
        MP7PickUp = GameObject.FindGameObjectsWithTag("MP7PickUp");
       
        
        
	}

    // Update is called once per frame
    void Update()
    {
        //Check All The UMP PickUpAble In The Array
        for (int i = 0; i < UMPPickUp.Length; i++)
        {
            //Checks Distance Between Player And Weapon
            if (Vector3.Distance(transform.position, UMPPickUp*.transform.position) <= distToPickUp)*

{
//Activating Pick Up Text
PickUpText.text = “Press E To Pickup HK-UMP”;
PickUpText.gameObject.SetActive(true);
if (Input.GetKeyUp(KeyCode.E))
{
//Which Weapon We Should Throw?
if (WeaponHoldIndex == 0)
GunToThrow = M21PickUpThrow;
if (WeaponHoldIndex == 1)
GunToThrow = UMPPickUpThrow;
if (WeaponHoldIndex == 2)
GunToThrow = MP7PickUpThrow;
//Weapon Throwing
Rigidbody thr = Instantiate(GunToThrow, new Vector3(UMPPickUp.transform.position.x, UMPPickUp_.transform.position.y + 3, UMPPickUp*.transform.position.z), Quaternion.identity) as Rigidbody;
//Initialize Arrays After Weapon Throwning*

M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);
//Activating The Gun We Just PickedUp And Desactivating Others
UMPModel.SetActive(true);
MP7Model.SetActive(false);
M21Model.SetActive(false);
//Playing The PickUp Animations
UMPModel.animation.Play(“PickUp”);
//Set The Weapon Index
WeaponHoldIndex = 1;
//Desactivate pickup text
PickUpText.gameObject.SetActive(false);
//Get Rid Of The Weapon (Cannot Delete Item From An Array)
UMPPickUp*.transform.position = new Vector3(999999, 999999, 999999);*
}
}
else PickUpText.gameObject.SetActive(false);
}
//Same As Above
for (int i = 0; i < M21PickUp.Length; i++)
{
if (Vector3.Distance(transform.position, M21PickUp*.transform.position) <= distToPickUp)*
{
PickUpText.text = “Press E To Pickup M21 Barret”;
PickUpText.gameObject.SetActive(true);
if (Input.GetKeyUp(KeyCode.E))
{
if (WeaponHoldIndex == 0)
GunToThrow = M21PickUpThrow;
if (WeaponHoldIndex == 1)
GunToThrow = UMPPickUpThrow;
if (WeaponHoldIndex == 2)
GunToThrow = MP7PickUpThrow;
Rigidbody thr = Instantiate(GunToThrow, new Vector3(M21PickUp.transform.position.x, M21PickUp.transform.position.y + 3, M21PickUp*.transform.position.z), Quaternion.identity) as Rigidbody;
M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);
UMPModel.SetActive(false);
MP7Model.SetActive(false);
M21Model.SetActive(true);
M21Model.animation.Play(“PickUp”);
WeaponHoldIndex = 0;
PickUpText.gameObject.SetActive(false);
M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);_

_M21PickUp.transform.position = new Vector3(999999, 999999, 999999);
}
}
else PickUpText.gameObject.SetActive(false);
}*_

* }*

}

To answer your question: sorry, there is no way to call two functions at the same time. Even if you did, you would run into what’s known as a ‘race condition’: basically, whichever function happened to run faster would write to variables (and access their values) before the other function, and with race conditions there’s no way to know which would finish first - definitely not something to do on purpose.

You’ll have to find another way to deal with picking up weapons without dropping weapons you’re already carrying. There are numerous ways to do so, and I suggest you learn a bit more about programming before you continue.

I suggest you a different approach: instead of getting the pickable weapons in arrays and measuring their distance each frame, you could add a sphere collider to each weapon prefab (just add it, don’t replace the original collider) and set Is Trigger, then use OnTriggerEnter in the player script to detect when the player is close to one of them - this simplifies and speed up things a lot.

I tried to adapt your current script to this approach - hope it can help you:

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic;

public class PickUpWeapon : MonoBehaviour {
    //Text To Ask The User If He Wants To Pickup The Weapon
    public GUIText PickUpText;
    //Weapon In Player Hands Variables
    public GameObject M21Model; 
    public GameObject UMPModel;
    public GameObject MP7Model;
    //The Weapon We Throw When We Get Another One
    public Rigidbody M21PickUpThrow;
    public Rigidbody UMPPickUpThrow;
    public Rigidbody MP7PickUpThrow;
	// Throw direction and force
	public Vector3 throwDirection = new Vector3(-0.2f, 0.8f, 0.0f);
	public float throwForce = 300.0f;

    //The Gun We Should Throw
    private Rigidbody GunToThrow;
    // Current Weapon We Are Holding
	private GameObject curWeapon;
	private int WeaponHoldIndex;
	// Weapon to be picked
	private GameObject pickWeapon;
	private int pickNum = -1; // assign null value to pickNum
 
    // Use this for initialization
    void Start () {
        //Config The Current Weapon
        if (M21Model.activeSelf)
            SelWeapon(0);
		else
        if (UMPModel.activeSelf)
            SelWeapon(1);
		else
        if (MP7Model.activeSelf)
            SelWeapon(2);
        PickUpText.text = ""; // clear message
    }
	
	void SelWeapon(int num){
		M21Model.SetActive(num == 1);
		UMPModel.SetActive(num == 2);
		MP7Model.SetActive(num == 3);
		WeaponHoldIndex = num;
		switch (num){
			case 1:
				GunToThrow = M21PickUpThrow;
				curWeapon = M21Model;
				break;
			case 2:
				GunToThrow = UMPPickUpThrow;
				curWeapon = UMPModel;
				break;
			case 3:
				GunToThrow = MP7PickUpThrow;
				curWeapon = MP7Model;
				break;
		}
	}
	
	void OnTriggerEnter(Collider other){
		int num = -1;
		switch (other.tag){
			case "M21PickUp":
				PickUpText.text = "Press E to pickup M21";
				pickNum = 0;
				break;
			case "UMPPickUp":
				PickUpText.text = "Press E to pickup UMP";
				pickNum = 1;
				break;
			case "MP7PickUp":
				PickUpText.text = "Press E to pickup MP7";
				pickNum = 2;
				break;
		}
		if (num >= 0){
			pickNum = num;
			pickWeapon = other.gameObject;
		}
	}
	
	void OnTriggerExit(){
		if (pickWeapon){
			PickUpText.text = ""; // clear the message...
			pickWeapon = null; // and the pickable weapon reference
			pickNum = -1;
		}
	}
	
    void Update(){
		// if some weapon was picked and E pressed...
		if (pickNum >= 0 && Input.GetKeyUp("e")){
			Destroy(pickWeapon); // destroy the model you've picked up
			PickUpText.text = ""; // clear the message
			// create the throwable version of curWeapon...
			Rigidbody thr = Instantiate(GunToThrow, curWeapon.transform.position, curWeapon.transform.rotation) as Rigidbody;
			thr.AddForce(throwForce * transform.TransformDirection(throwDirection)); // and throw it away
			SelWeapon(pickNum); // select the new weapon...
			pickNum = -1; // null pickNum
			curWeapon.animation.Play("PickUp"); // and play its pickup animation
		}
	}
	
}