Help with finding a tag Script

Hello

I have a script placed on my weapon items that adds a script based on the tag of a parent two levels up in the hierarchy, which would be a weaponmount on a character. And it works fine for that purpose. However, if I just want to throw the weapon in the scene my variable wpnChk will return a null because there is no second level parent as you can see in the following c# code:

using UnityEngine;
using System.Collections;

public class WeaponInit : MonoBehaviour {
	
	private string wpnCheck;
		
	void Start () {
		
		wpnCheck = transform.parent.parent.tag;
		
		if (wpnCheck == "Enemy")
			gameObject.AddComponent ("EnemyWeaponScript");
				
		if (wpnCheck == "Ally")
			gameObject.AddComponent ("AllyWeaponScript");	
		
		if (wpnCheck == "Player")
			gameObject.AddComponent ("WeaponScript");
		
		Debug.LogWarning(wpnCheck);
		
	}
}

So, to fix that I removed declaring the wpnCheck variable and attempted an if else statement in its place:

	if (wpnCheck != null)	
		wpnCheck = transform.parent.parent.tag;
	else		
		wpnCheck = transform.parent.tag;

Now it only returns the else statement even if the weapon mounts on a character. What I am trying to do with this script is look at the tag of the second level parent and then add the appropriate script or no script if it is null.

Does anyone have a better approach or can point me in the right direction of what I am doing wrong?

Thanks.

[Edit by Berenger : Code formatting]

You could check if their is a parent and a parent parent. Or send a message upwards asking for the tag : SendMessageUpwards( “GiMeYourTagDude”, dontRequiereEtc ); and on the parent parent : BroadcastMessage( “HereYouGoBro”, tag, dontRequiereEtc ).