How do I access/use the variable inside a script when the script name is unknown but objectname is?

I have placed a box collider as a child object to my AI player. This represents my player’s weapon. This weapon’s box collider sticks out in front of the AI player, and when it collides with an enemy it knows it can start attacking it.

This is all fine and dandy, except I don’t know how to access the damage variable within my “Sword” object’s script, also called “Sword”. I cannot explicitly state “Sword” with a GetComponenet because I never know what weapon the AI player will be starting the game with.

This is how I’m currently pulling the weapon game object by tag (important):

void Start()
{
    weapon = FindWeapon();
    Debug.Log(weapon.tag + weapon.name);
}

 GameObject FindWeapon()
    {
        GameObject weapon = null;
        foreach (Transform child in transform)
        {
            if (child.tag == "Weapon")
            {
                weapon = child.gameObject;
            }
            else
            {
                weapon = null;
            }
        }
        return weapon;
    }

So what do I do now to actually access the variable named “damage” inside the “Sword” script within the child object “Sword”? I will never know what the actual name of the weapon or script might be, so that’s why I find it on start. However, I plan on making the weapon names always match the script within it (Sword/Sword).

Do an abstract class for all weapons and inherit it with the specific ones. this way a global getComponent(WeaponScript) would work.

I’m doing the exact same thing in a prototype I’m making at the moment:

I have items, that I can pick up, and a type of item would be a weapon. I can also interact with items, so my items extend from my “Interact” class.

So in effect I can pick up all instances of “Weapon” even though the pickup function is not in the “Weapon” class, but in the “Item” class! Neat huh?

So in your case, you might want to define your sword as Sword : Weapon
and in your “Weapon” class, define your damage variable as public int/float damage

 public  class Item : Interact    //
    {
        public ItemSettings settings;

        [System.Serializable]
        public class ItemSettings{
            public Vector3 pickupRotation;
            public Vector3 pickupPosition;
            public float maxForceMod;
        }
        public Rigidbody body;

        void Start()
        {
            body = GetComponent<Rigidbody>();
        }


        public void Pickup()
        {
            FindObjectOfType<PlayerInteraction>().PickupItem(this);

        }
}

//Next file:

 public class Weapon : Item //<---- notice how weapon extends item? That means weapon has all the                    //variables declared in "item", and Weapon is a type of Item.
    {
        public float attackFraction = 0;
        public bool attacking = false;
        public enum animationName {
            SwingAttack, SmashAttack
        };
        public animationName animName;
}

using UnityEngine;
using System.Collections;

// attach this to the player
public class Test : MonoBehaviour
{
	GameObject[] allWeapons;
	GameObject myWeapon;


	// Use this for initialization
	void Start ()
	{
		myWeapon = GetWeapon ();

		//we got the weapon
		Debug.Log(myWeapon.name);

	}
	
	// Update is called once per frame
	GameObject GetWeapon()
	{

		allWeapons = GameObject.FindGameObjectsWithTag ("Weapon");

		foreach (GameObject g in allWeapons) 
		{
			Debug.Log (g.name);
		}
		int MyIndex = Random.Range(0,(allWeapons.Length));

		Debug.Log ("SELECTED" + allWeapons[MyIndex].name);
		return allWeapons[MyIndex];
	}

	public void Update()
	{
		int damage = myWeapon.GetComponent<SimpleWeaponBaseClass> ().damage;
		Debug.Log (damage);
	}
}