Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by importguru88 · Jun 09, 2016 at 03:56 PM · c#parentdamagerootapply

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);
             }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image importguru88 · Jun 09, 2016 at 04:30 PM 0
Share

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

}

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I am wondering how to save a int in the background 2 Answers

Reading a var from variable parents 1 Answer

Setting parent through custom script not working. 0 Answers

error CS0103: The name `yourCharacter' does not exist in the current context 1 Answer

order of scripts 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges