Why Wont This Work?

I’m trying to make a weapon pickup script for my FPS but i’v ran into a very strange problem. When you press c the engine searches for every game object tagged “gun” and stores them in a array. Then after that it determines what the name of the object is, and if the name of the object is not equal to either gun1 or gun2 it should print out a line. The problem is that the name IS EQUAL TO gun1 or gun2, so why is it printing?

Here is the script can someone please help me out on this:

var gun1 : GameObject;
var gun2 : GameObject;

function Update(){
		var weapons : GameObject[] = GameObject.FindGameObjectsWithTag("gun");
		for(var closestWeapon : GameObject in weapons){
			if(closestWeapon.name != gun1.name || gun2.name){
				print(closestWeapon.name + " is not equal to " + gun1.name + " or " + gun2.name);

I’m not entirely sure, but I think you if() statement checks (if the closest weapons’ name is not equal to gun1’s name OR if gun2 has a name) print(…).

Try if(closestWeapon.name != gun1.name && closestWeapon.name != gun2.name){

The issue is this line here:

if(closestWeapon.name != gun1.name || gun2.name){

You’re checking if the closestWeapon.name does not equal gun1.name OR gun2.name. If you’re closestWeapon.name is not equal to gun1 or gun2, this code is going to execute. So even though your closestWeapon.name is equal to gun1, it is not equal to gun2, so the code executes anyway.

You can solve this by either do an AND check:

if(closestWeapon.name != gun1.name && closesteWeapon.name != gun2.name){

or you can check for one gun at a time:

if(closestWeapon.name != gun1.name ){
//check gun1.name and do something
else if( closestWeapon.name != gun2.name) {
//check gun2.name and so something