How do I get the access of the parent component or root parent in Unity3d C#
I am having a problem applying damage to the enemy ai . The health2016 script is the enemy ai health script . I need to get access of the parent in the shootable script from the health2016 script or root parent. So I can apply damage to the enemy ai . The amount of damage is already set in the shootable weapon script. I don't know how to code that in my health2016 script. In the Hitscan fire function its Health hit. Health hit is a networking health script and don't want to use that. I did change health hit to health2016 and apply damage to enemy ai and weapon still didn't do any damage . So I change the code back to health hit. So gotta keep it coded health hit. I am making networking game. I just need to access the parent in the hitscan fire function from health2016 , so I can apply damage to enemy ai.
using UnityEngine; using System.Collections;
public class health2016 : MonoBehaviour { public int Health;
void Start () {
}
public void TakeDamage (int amount) {
Health -= amount;
}
}
using UnityEngine;
namespace Opsive.ThirdPersonController { ///
/// Any weapon that can be shot. This includes pistols, rocket launchers, bow and arrows, etc. /// public class ShootableWeapon : Weapon, IFlashlightUsable, ILaserSightUsable {private void HitscanFire() { // Cast a ray between the fire point and the position found by the crosshairs camera ray. var fireDirection = FireDirection(); if (Physics.Raycast(m_FirePoint.position, fireDirection, out m_RaycastHit, m_FireRange, m_HitscanImpactLayers.value)) {
// Execute any custom events.
if (!string.IsNullOrEmpty(m_HitscanDamageEvent)) {
EventHandler.ExecuteEvent(m_RaycastHit.collider.gameObject, m_HitscanDamageEvent, m_HitscanDamageAmount, m_RaycastHit.point, m_RaycastHit.normal * -m_HitscanImpactForce);
}
// If the Health component exists it will apply a force to the rigidbody in addition to deducting the health. Otherwise just apply the force to the rigidbody.
Health hitHealth;
if ((hitHealth = m_RaycastHit.transform.GetComponentInParent<Health>()) != null) {
hitHealth.Damage(m_HitscanDamageAmount, m_RaycastHit.point, fireDirection * m_HitscanImpactForce, m_Character);
} else if (m_HitscanImpactForce > 0 && m_RaycastHit.rigidbody != null && !m_RaycastHit.rigidbody.isKinematic) {
m_RaycastHit.rigidbody.AddForceAtPosition(fireDirection * m_HitscanImpactForce, m_RaycastHit.point);
}
Answer by FortisVenaliter · Jun 09, 2016 at 04:06 PM
You need to format your code properly if you want people to be able to analyze it. It's hard to look at word soup.
That being said, if you just need to get the parent object, you can do so through the transform.parent property.
How do I code that on the health2016 script so I can apply damage to enemy ai. What do I need to change ?
using UnityEngine; using System.Collections;
public class health2016 : $$anonymous$$onoBehaviour { public int Health;
void Start () {
}
public void TakeDamage (int amount) {
Health -= amount;
}
}