Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Use Variables from another script

Discussion in 'Scripting' started by DemonicProgramer, May 22, 2024.

  1. DemonicProgramer

    DemonicProgramer

    Joined:
    Aug 26, 2023
    Posts:
    5
    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
    Code (CSharp):
    1.     // Names Of Objects it hit
    2.     string[] HitNames = new string[1000];
    Function has these parameters

    Code (CSharp):
    1. Vector3 center, float radius, int Level

    Inside Function

    Code (CSharp):
    1.         // Hit Counter
    2.  
    3.         int Hitcounter = 0;
    4.  
    5.  
    6.         // Objects
    7.  
    8.         Collider[] hitColliders = Physics.OverlapSphere(center, radius);
    9.  
    10.         foreach (var hitCollider in hitColliders)
    11.  
    12.         {
    13.  
    14.             // Check if the gameObject has the tag DamageReceiver
    15.  
    16.             if (hitCollider.gameObject.CompareTag("DamageReceiver"))
    17.  
    18.             {
    19.  
    20.                 // Get Name
    21.  
    22.                 HitNames[Hitcounter] = hitCollider.gameObject.name;
    23.  
    24.  
    25.  
    26.                 // Increase Hit Counter
    27.  
    28.                 Hitcounter++;
    29.  
    30.             }
    31.  
    32.         }
    33.  
    34.  
    35.  
    36.         // Set Counter
    37.  
    38.         int Counter = 0;
    39.  
    40.  
    41.  
    42.         // Raycast
    43.  
    44.         foreach (var Name in HitNames)
    45.  
    46.         {
    47.  
    48.             // Find Game Object Position
    49.  
    50.             Vector3 objectPosition = GameObject.Find(Name).transform.position;
    51.  
    52.  
    53.  
    54.             // Find Direction
    55.  
    56.             Vector3 Direction = (objectPosition - center);
    57.  
    58.  
    59.  
    60.             // Collider
    61.  
    62.             RaycastHit RayCastCollider;
    63.  
    64.  
    65.  
    66.             // Raycast
    67.  
    68.             bool RayCast = Physics.Raycast(center, Direction, out RayCastCollider, 30);
    69.  
    70.  
    71.  
    72.             // Compare Name to expected name
    73.  
    74.             if (RayCastCollider.collider.gameObject.name == Name)
    75.  
    76.             {
    77.  
    78.                 // Script name as string
    79.  
    80.                 string ScriptNameStr = RayCastCollider.collider.gameObject.name + "Attributes";
    81.  
    82.  
    83.  
    84.                 // Component
    85.  
    86.                 Component ScriptName = RayCastCollider.collider.gameObject.GetComponent(ScriptNameStr);
    87.  
    88.              
    89.  
    90.                 // Damage
    91.  
    92.                 ScriptName.HP = ScriptName.HP - DamageCal(Level);
    93.  
    94.             }
    95.  
    96.          
    97.  
    98.             // Increase Counter
    99.  
    100.             Counter++;
    101.  
    102.         }
    103.  
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    838
    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:
    Code (csharp):
    1. if (obj.GetComponent() is not DataComponent dataComponent
    2.     || dataComponent == null)
    3. {
    4.   // component was null or not a DataComponent
    5.   throw new Exception();
    6. }
    7. 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.
    Code (csharp):
    1. interface ILivingObject
    2. {
    3.   float HP { get; }
    4. }
    5.  
    6. class DataComponent : MonoBehaviour, ILivingObject
    7. {
    8.   // serialized field
    9.   public float hp;
    10.  
    11.   // ILivingObject implementation
    12.   public float HP => hp;
    13. }
     
    spiney199 likes this.
  3. DemonicProgramer

    DemonicProgramer

    Joined:
    Aug 26, 2023
    Posts:
    5
    Thank you for the help I fixed it.