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 CutterTheCoder · Dec 22, 2015 at 06:13 AM · errorreferencing

NullReferenceException: Object reference not set to an instance of an Object

I have a hand attack, when the enemy is in the hand collider i will deal damage to the enemy, then want to check which way i am facing taking the boolean from another script on the same object, I try reference the component in the awake function, but when i attack the enemy and it gets to the if statement it says the error about the condition 'playerMovement.facingRight'

Any Advice?

 public class handAttack : MonoBehaviour {
 
     public int damage = 20;
     public float timeBetweenAttacks = 0.50f;
     Rigidbody2D enemyRB;
 
     PlayerMovement playerMovement;
     float timer;
     // Use this for initialization
     void Awake ()
     {
         playerMovement = GetComponent<PlayerMovement>();
     }
     
     // Update is called once per frame
     void Update ()
     {
 
         timer += Time.deltaTime;
 
 
     }
     void ResetAttack()
     {
         timer = 0f;
 
 
     }
     void OnTriggerStay2D (Collider2D other)
     {
         if (Input.GetButton("Fire1") && timer >= timeBetweenAttacks && other.gameObject.CompareTag("Enemy"))
         {
             
             EnemyHealth enemyHealth = other.gameObject.GetComponent<EnemyHealth>();
             
 
             enemyRB = other.gameObject.GetComponent<Rigidbody2D>();
             ResetAttack(); //Allows a 'rate of fire'
             enemyHealth.TakeDamage(damage);
             if(playerMovement.facingRight == true)
             {
                 enemyRB.AddForce(new Vector2(10f, 0f));
             }
             if (playerMovement.facingRight == false)
             {
                 enemyRB.AddForce(new Vector2(-10f, 0f));
             }
         }
     }
 
 }
 


Comment
Add comment · Show 1
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 Positive7 · Dec 22, 2015 at 08:26 AM 0
Share

After line 37 Debug.Log(enemyRB.name); Also using Input inside OnTriggers is a bad idea.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CutterTheCoder · Dec 22, 2015 at 07:39 PM

Found the problem, the problem was i was referencing the script that was attached to the gameObject when the script for attacking was attatched to the child(hand)

Comment
Add comment · 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

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

37 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

Related Questions

Using a reference for another script in a method issue. 0 Answers

Having referencing Problems while creating a tile brush for an editor in unity 0 Answers

object reference not set to an instance of an object Player.Damge (Single count) (at Assets/Player.js:31) 0 Answers

NullReference. Need Help. 1 Answer

Unity3d Script does not work anymore 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