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++;
}