Hi guys, i would like to know if its possible to a child object access a function that is in the parent script to be used in the animation event. Appreciate
Sure, just try GetComponentInParent() to get the script component. You can also set a reference to the parent object in the inspector and use GetComponent() if the parent is multiple levels away and if it’s possible to encounter more than one instance of the script.
If it’s only the object’s direct parent you’re interested in, transform.parent.GetComponent()
also works. Be sure to read this page if you haven’t already.
So the animator is in Mummy, where i create the animation event and the script that i need acces in in player(script called NPCBehavior and the function ApplyDamage(float damage), so i need create a script in the mummy to do it? and just use in start method:
GetComponentInParent().ApplyDamage();
Cuz i get an error doing that
The animator talks to scripts on the same GameObject that the Animator is attached. So yes, you’ll need a script on ‘Mummy’ for this.
As for getting a reference to the parent object can be done with GetComponentInParent, or by directly referencing it by including a property on the script for the script type and dragging it onto it in the inspector, or you could use some sort of event/messaging system, or any other variation of that.
GetComponentInParent/Child is not recursive, right? So I think it only checks first generation, not all the way to the tree top or bottom.
It is recursive:
So i hv this script i want to access ApplyDamage function, i hv to create an script in my mummy with wt type of code, i really dont get it and its simple i know -.- but i get error after error and still cant access the ApplyDamage function in my animation event…
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NPCBehavior : MonoBehaviour
{
//variables
protected Vector3 targetToWalk;
protected NavMeshAgent agent;
//stats
public float MaxHp;
private float currentHp;
public int attackPower;
public int armor;
public float attackRate;
private float currentAttackRate;
public float attackRange;
public float hpRegen;
//UI
public GameObject lifeBarPrefab;
public Transform lifeBarPosition;
private Slider lifeBar;
protected Animator anim;
// Use this for initialization
virtual protected void Start () {
agent = GetComponent<NavMeshAgent>();
targetToWalk = transform.position;
currentHp = MaxHp;
anim = GetComponentInChildren<Animator>();
//life bar
GameObject tempLifeBar = Instantiate(lifeBarPrefab, lifeBarPosition.position, transform.rotation) as GameObject;
tempLifeBar.transform.SetParent (lifeBarPosition, true);
lifeBar = tempLifeBar.GetComponentInChildren<Slider> ();
lifeBar.maxValue = MaxHp;
}
// Update is called once per frame
virtual protected void Update () {
//animations
if (anim != null) {
anim.SetFloat ("velocity", agent.velocity.magnitude);
}
lifeBar.value = currentHp;
//lifeBarPosition.LookAt (Camera.main.transform);
if (targetToWalk != transform.position) {
agent.SetDestination (targetToWalk);
}
if (currentAttackRate < attackRate) {
currentAttackRate += Time.deltaTime;
}
}
public void Attack(NPCBehavior target){
if (CanAttack ()) {
target.ApplyDamage (attackPower);
currentAttackRate = 0;
if (anim != null) {
anim.SetTrigger ("attack");
}
}
}
public bool CanAttack(){
return currentAttackRate >= attackRate;
}
public void ApplyDamage(float damage){
if (damage > armor) {
currentHp -= damage;
if (currentHp <= 0) {
Die ();
}
if (anim != null) {
anim.SetTrigger ("hit");
}
}
}
public void Die(){
Destroy (gameObject);
}
}