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 sazazezer · Jul 26, 2018 at 02:41 PM · instantiateunity 2dparent-child

Referencing a script of a parent of a instantiated instance from another instantiated instance

I'm making a parrying system in a 2d game on unity. The idea is that when the enemy attacks the player can parry and that stuns the enemy (and vice versa). At this point when the player parries it instantiates a hitbox parrying instance that can collide with with the enemy's instantiated hitbox attacking instance. For this i have the function below:

 void OnCollisionEnter2D(Collision2D target){
     if(target.gameObject.tag == targetTag){
         if(actionType == ActionType.Attacking && target.gameObject.GetComponent<SwordHitbox>().actionType == ActionType.Attacking){
             Debug.Log("Attack hits attack");
         }
         if(actionType == ActionType.Attacking && target.gameObject.GetComponent<SwordHitbox>().actionType == ActionType.Parrying){
             Debug.Log("Attack hits parry");
             if(transform.parent.gameObject.tag == playerTag){
                 Debug.Log("Enemy parries player");
                transform.parent.gameObject.GetComponent<Parried>().GetParried(); //this version works!!
                 }
         }
         if(actionType == ActionType.Parrying && target.gameObject.GetComponent<SwordHitbox>().actionType == ActionType.Attacking){
           //  if(transform.parent.gameObject.tag == enemyTag){                                                       //commented out while trying to fix target issue
             Debug.Log("Parry hits Attack");                                                                              //shows in the console log
             target.transform.parent.gameObject.GetComponent<EnemyParried>().GetParried(); //this version doesn't work!!
           //  }
         }
     }
 }

The last two lines are where the problem is. When the enemy parries the player on line ten it works fine but when the player parries the enemy i get the message below, which is making me feel that the last line's dot walking trail isn't correct and it's trying to refer to the object of the parent rather than the instance.

 NullReferenceException: Object reference not set to an instance of an object SwordHitbox.OnCollisionEnter2D (UnityEngine.Collision2D target) (at Assets/Toolkit/PlayerTools/Scripts/Items/SwordHitbox.cs:31)

The use of target dot walking works fine when referring to the instance itself (see line 3). I've tried several permutations of the dot walk including GetComponentInParent but have had no joy. Can anyone make any suggestions as to where i'm going wrong? Thanks in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I want to generate obj at random places which can move in random speed in X axis.,How to generate a object at random position with varying speed. 1 Answer

Parent object to instantiate 1 Answer

Instantiating two prefabs instead one (Bug) 0 Answers

Child Instantiated GameObject to GameObject hit by raycast 1 Answer

How can I Instantiate only one GameObject Using PhotonNetwork. 0 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