Shouldn`t unity read lines in this order correct ?

I want to make weapon replace system and I have a weapon manager class that holds player`s weapons and a weapon holder which holds the weapons , I have this code in the weapon holder :

using UnityEngine;
using System.Collections;

public class WeaponHolder : MonoBehaviour {

	public Weapon weapon;
	Weapon tempWeapon;
	WeaponManager manager;

	void Start () 
	{
		
	}
	

	void Update () 
	{
		
	}

	void OnTriggerStay2D(Collider2D coll)
	{
		if(coll.gameObject.tag == "Player")
		{
			Debug.Log("Player entered");
			manager = coll.GetComponentInChildren<WeaponManager>();
			Debug.Log (manager.weapon1);
			if(Input.GetButtonDown("Replace1"))
			{
				tempWeapon = manager.weapon1;
				Debug.Log("temp is " + tempWeapon);
				manager.weapon1 = null;
				manager.weapon1 = weapon;
				Debug.Log("manager weapon is " + manager.weapon1);
				Debug.Log("temp is " + tempWeapon);
				Debug.Log("weapon is " + weapon);
				weapon = tempWeapon;
				Debug.Log("weapon is " + weapon);
				Debug.Log("manager weapon is " + manager.weapon1);
			}
		}
	}
}

The idea is simple , place the current weapon of the player in the temporary var , then put the weapon the holder has in the players weapon slot then put the temporary var ( which was the weapon the player had ) in the current holder slot , so it replaces weapons , but for some reason at the last line when I change the weapon to tempWeapon it changes the manager.weapon1 also , although the manager.weapon1 line was before it so it should had the old value of weapon … this leads that console prints that each frame it has the weapon that should be replaced , and the new weapon becomes the weapon of the player for only one frame.

Yes, that should work just fine. Btw things like the tempWeapon variable should be local: Also for performance reasons you should do the GetComponentInChildren inside the if unless you really need the reference before you press the button.

using UnityEngine;
using System.Collections;
 
public class WeaponHolder : MonoBehaviour
{
    public Weapon weapon;
     
    void OnTriggerStay2D(Collider2D coll)
    {
        if(coll.gameObject.tag == "Player")
        {
            Debug.Log("Player entered");
            if(Input.GetButtonDown("Replace1"))
            {
                WeaponManager manager = coll.GetComponentInChildren<WeaponManager>();
                Weapon tempWeapon = manager.weapon1;
                manager.weapon1 = weapon;
                weapon = tempWeapon;
                Debug.Log("weapon is " + weapon);
                Debug.Log("manager weapon is " + manager.weapon1);
            }
        }
    }
}

This will work. If not you might change the variables in another script. Is weapon1 a public variable or is it a property? What exactly is “Weapon” ? is it a custom class or a class derived from MonoBehaviour?