Use Variables from another script

I would like to use a variable HP (HitPoints) from another script but the name of the script is defined by the gameObject that the second raycast touched plus “Attributes”. After acquiring the variable I need to subtract the amount of damage taken from the spell.

Notes

  • First Raycast finds what is effected by the spell and filters out gameobjects that are not damage receivers (Line 8)

  • Second Raycast is used to see if there is a object blocking the sphere (Ex: A wall is in between player and spell) (Line 68)

  • ScriptNameStr creates name of the script (Line 80)

  • ScriptName Is the name of the script class (Line 92)

  • Hp Is Hit Point variable (Line 92)

Current error occurs when subtracting damage from hp (On underlined parts)

ScriptName.HP = ScriptName.HP - DamageCal(Level);

Error is

Same Error just zoomed in

Current code:

Outside Function

    // Names Of Objects it hit
    string[] HitNames = new string[1000];

Function has these parameters

Vector3 center, float radius, int Level

Inside Function

        // Hit Counter

        int Hitcounter = 0;


        // Objects

        Collider[] hitColliders = Physics.OverlapSphere(center, radius);

        foreach (var hitCollider in hitColliders)

        {

            // Check if the gameObject has the tag DamageReceiver

            if (hitCollider.gameObject.CompareTag("DamageReceiver"))

            {

                // Get Name

                HitNames[Hitcounter] = hitCollider.gameObject.name;



                // Increase Hit Counter

                Hitcounter++;

            }

        }



        // Set Counter

        int Counter = 0;



        // Raycast

        foreach (var Name in HitNames)

        {

            // Find Game Object Position

            Vector3 objectPosition = GameObject.Find(Name).transform.position;



            // Find Direction

            Vector3 Direction = (objectPosition - center);



            // Collider

            RaycastHit RayCastCollider;



            // Raycast

            bool RayCast = Physics.Raycast(center, Direction, out RayCastCollider, 30);



            // Compare Name to expected name

            if (RayCastCollider.collider.gameObject.name == Name)

            {

                // Script name as string

                string ScriptNameStr = RayCastCollider.collider.gameObject.name + "Attributes";



                // Component

                Component ScriptName = RayCastCollider.collider.gameObject.GetComponent(ScriptNameStr);

             

                // Damage

                ScriptName.HP = ScriptName.HP - DamageCal(Level);

            }

         

            // Increase Counter

            Counter++;

        }

You can’t arbitrarily ask for “HP” from a variable of type Component, since that type doesn’t define such a property or field. You need to access the member from an appropriately typed expression. For example, if HP were defined on a custom component named DataComponent, you would want to make sure that you cast the variable to that type before accessing:

if (obj.GetComponent(…) is not DataComponent dataComponent
    || dataComponent == null)
{
  // component was null or not a DataComponent
  throw new Exception();
}
var hp = dataComponent.HP;

If you didn’t specify the field or property in a common base class, you can use an interface to allow types from different type hierarchies to implement members that you can access by casting them to the interface type.

interface ILivingObject
{
  float HP { get; }
}

class DataComponent : MonoBehaviour, ILivingObject
{
  // serialized field
  public float hp;

  // ILivingObject implementation
  public float HP => hp;
}
1 Like

Thank you for the help I fixed it.