• 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 /
avatar image
0
Question by Darkkingdom · Feb 08, 2015 at 09:13 PM · c#triggerparent

Trigger problem with bullet

Hey guys,

I have a small problem with my bullet script. So let's start with the script:

     void Start () {
 
         if (transform.parent != null) {
             fightingRange = transform.parent.gameObject.GetComponent<C1FightingRange> ();
             stats = transform.parent.gameObject.GetComponent<C1Stats> ();        
         }        
         
     }
 
 void OnTriggerEnter2D(Collider2D coll)
     {
         if (coll.tag != "Player" && coll.tag != "Bullet") {
             isColliding = true;
             rigidbody2D.velocity = Vector2.zero;
             
             if (coll.tag == "Enemy") {
                 
                 totalDamage = (stats.baseDamage * fightingRange.attackDamage)/coll.transform.gameObject.GetComponent<E1Stats> ().baseDefense;
                 
                 if (totalDamage < 0) {
                     totalDamage = 0;
                 }
                 coll.transform.gameObject.GetComponent<E1Stats> ().baseLife = coll.transform.gameObject.GetComponent<E1Stats> ().baseLife - totalDamage;
                 
             }
         }
     }

My bullet is a child of the player. I instanciate it like this:

 bullet =  Instantiate(bulletPrefab, bulletSpawnPosistion,  Quaternion.identity) as GameObject;
     bullet.transform.parent = transform;

The script as it work's fine. But the only problem is then i stand very closely to my enemy so that the bullet spawns in the collider of the enemy i get a "NullReferenceException: Object reference not set to an instance of an object" error in the the following line:

 totalDamage = (stats.baseDamage * fightingRange.attackDamage)/coll.transform.gameObject.GetComponent<E1Stats> ().baseDefense;


To be exact the stats.baseDamage part. And I could't set the "Start" part into "Awake" because then I get a "NullReferenceException: Object reference not set to an instance of an object" error from the "transform.parent.gameObject.GetComponent ();" line.

Soooo what am I doing wrong?^^

Thank you very much for your time :)

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

Answer by nirharpaz · Feb 08, 2015 at 09:37 PM

can you print:

coll coll.transform coll.transform.gameObject coll.transform.gameObject.GetComponent coll.transform.gameObject.GetComponent ().baseDefense stats.baseDamage fightingRange.attackDamage

please? see which one is null?

my guess is that since you spawned inside the trigger collider, you may have not entered it. in a way that there is no coll in there. im not sure about it but if you find out that coll is null you may wanna try to add some OnTriggerStay2D(Collider2D coll) action to fix the situation

Comment
Add comment · Show 4 · 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 Darkkingdom · Feb 09, 2015 at 06:09 PM 0
Share

Ok at first thanks for you help^^

I printed as you wish and the only 2 null's are in stats.baseDamage and fightingRange.attackDamage. It seem's like the coll works right... that's strange.

avatar image nirharpaz · Feb 10, 2015 at 08:48 AM 0
Share

no its not if stats.baseDamage and fightingRange.attackDamage are null

  totalDamage = (stats.baseDamage * fightingRange.attackDamage)/coll.transform.gameObject.GetComponent<E1Stats> ().baseDefense;

What you have is totalDamage=null/null. if it was only a null you would have recieved that error latter. but since you are doing an arithmetic action with nulls you get it now.

tThis is why only now you get this error. that's what i assume at least check why those functions/variables return null.

solve that issue and see if the getComponent thing is solved

maybe, if you will find it useful to deal with the varialbe (say baseDamage)by adding Getter and Setter functions to the script that hold it.

 float GetBaseDamage()
 {
  return baseDamage;
 }
 
 void SetBaseDamage(float newBaseDamage )
 {
  baseDamage=newBaseDamage;
 }
 
avatar image Darkkingdom · Feb 12, 2015 at 09:50 AM 0
Share

Thank's again^^

to be honest I don't get why they are null. The values in the scripts are right and it work's like this but not in the first frame :( Even if I paste the line:

 stats = transform.parent.gameObject.GetComponent<C1Stats> ();

into the OnTriggerEnter it's null. I don't get it :( Is something wrong the way I initialize it? I tried it with get/set function to but the same error because of the stats line.

avatar image nirharpaz · Feb 13, 2015 at 03:56 PM 0
Share

is it c1state or E1Stats you were lookin for? try printing values in the process. every time you have a null, print the component before and check from there what's wrong.

If you need more help can you please write when and where is it initilized and on which function?

in what stage does the problematic OnTriggerEnter2D is kicked in? 1st frame or during the game somewhere?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How can I access a collider's parent's rigidbody? 1 Answer

Distribute terrain in zones 3 Answers

Play parent animation without affecting the child animation 1 Answer

Parent Script accessing Child components 1 Answer


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