- Home /
Cannot assign to 'Death' because its a method group
Hi I am new to coding and want to activate a rag-doll if the enemy's health value = 0, unfortunately in my code it says Cannot assign to Death because its a method group. If anyone can help my solve this issue I would greatly appreciate it. The code is down below( it is in the "void OnEnable()" section):
using UnityEngine; using System.Collections;
public class EnemyAI_Basic : MonoBehaviour { private EnemyAI_Basic enemyAI_Basic; Animator controller; float health; Animator anim; bool Alive = true; public bool Dead = false; int pointValue = 5; private Collider myCollider; private Rigidbody myRigidbody;
CapsuleCollider capsuleCollider;
void Start()
{
controller = GetComponentInParent<Animator>();
health = 40;
capsuleCollider = GetComponent<CapsuleCollider>();
anim = GetComponent<Animator>();
}
void Update()
{
if (!Dead)
{
anim.SetTrigger("Alive");
}
}
void Death()
{
Dead = true;
Alive = false;
capsuleCollider.isTrigger = true;
anim.SetTrigger("Dead");
Destroy(gameObject, 4f);
}
void OnEnable()
{
SetInitialReferences();
enemyAI_Basic.Death += ActivateRagdoll;
}
void OnDisable()
{
enemyAI_Basic.Death -= ActivateRagdoll;
}
void SetInitialReferences()
{
enemyAI_Basic = transform.root.GetComponent<EnemyAI_Basic>();
if (GetComponent<Collider>() != null)
{
myCollider = GetComponent<Collider>();
}
if (GetComponent<Rigidbody>() != null)
{
myRigidbody = GetComponent<Rigidbody>();
}
}
void ActivateRagdoll()
{
if (myRigidbody != null)
{
myRigidbody.isKinematic = false;
myRigidbody.useGravity = true;
}
if (myCollider != null)
{
myCollider.isTrigger = false;
myCollider.enabled = true;
}
}
}
Answer by mh_vitor · Mar 14, 2017 at 08:46 AM
Hello @Napter77,
This issue seems to be a syntax error. This syntax here:
object.Property = value;
is used when you want to set that property with a valid value.
In the case at hand, you used
object.Death = value;
but "Death" is a method. To send data to a method, you must pass it as an argument:
object.Death(value);
although you can't do that since the Death() method takes no argument.
If what you want is that when the object is disabled it becomes a rag-doll and then dies, use:
void OnDisable() { SetInitialReferences(); ActivateRagdoll(); Death(); }
Though, if I recall correctly, a disabled object disappears from the screen, but you can just move the code to another method if you ever need.
Answer by IgorAherne · Mar 13, 2017 at 12:57 AM
Add the following line: public delegate void ZeroArgFunctionPointer();
near one of your variables.
Afterwards, also declare a variable ZeroArgFunctionPointer functionsToCall;
Where it says enemyAI_Basic.Death += ActivateRagdoll;
, substitute it with functionsToCall += ActivateRagdoll;
do same for -=
part, during OnDisable()
Finally, at the end of Death(), but before Destroy(), launch all the methods attached to our delegate (in your case just the ActivateRagdoll), in the order they were added. functionsToCall.Invoke()
Notice, if you do
functionsToCall+=ActivateRagdoll;
functionsToCall+=ActivateRagdoll;
functionsToCall.Invoke();
this will invoke Activate Ragdoll twice, so be careful
Thanks I will check this out when I get home later today ;)
Your answer
Follow this Question
Related Questions
How to change CC script to Rigidbody script 1 Answer
Get result (force & torque) of AddForceAtPosition? 2 Answers
RigidBody.MovePosition seems jerky in movement 0 Answers
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers
Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers