Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Melee KnockBack using addforce

Discussion in '2D' started by Yan_Yan_, Dec 9, 2015.

  1. Yan_Yan_

    Yan_Yan_

    Joined:
    Nov 14, 2015
    Posts:
    6
    when I tried to use add force it will give me this error
    NullReferenceException: Object reference not set to an instance of an object
    Help will be appreciative
    Code (CSharp):
    1. public class enemyScript : MonoBehaviour {
    2.     public int currHealth;
    3.     public int maxHealth=30;
    4.     private Rigidbody2D enemeyRigidBody;
    5.     // Use this for initialization
    6.     void Start () {
    7.         currHealth = maxHealth;
    8.         Debug.Log(currHealth);
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update ()
    13.     {
    14.  
    15.         if (currHealth <= 0)
    16.         {
    17.  
    18.             Destroy(gameObject);
    19.         }
    20.          
    21.    }
    22.    
    23.  
    24.     public void underAttack(int damage)
    25.     {
    26.         currHealth -= damage;
    27.         Debug.Log(currHealth);
    28.          
    29.     }
    30.     void OnTriggerEnter2D(Collider2D col)
    31.     {
    32.         if (col.CompareTag("jabbing"))
    33.         {
    34.  
    35.             enemeyRigidBody.AddForce(Vector3.left * 10);
    36.         }
    37.     }
    38.    
    39. }
    40.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    I can't see that you actually set enemeyRigidBody anywhere in your code, which means that it is set to null. You need to assign it a value somewhere before the OnTriggerEnter2D event.
     
  3. Yan_Yan_

    Yan_Yan_

    Joined:
    Nov 14, 2015
    Posts:
    6
    oh crap thanks another question
    I got the code to work but I have another issue and it's with send message upward being called twice
    attack trigger
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class jabTrigger : MonoBehaviour {
    5.     public static int jabDamage = 2;
    6.     public GameObject animationObject;
    7.     GameObject col;
    8.     void Start()
    9.     {
    10.  
    11.          }
    12.     void OnTriggerEnter2D(Collider2D col)
    13.     {
    14.         if (col.isTrigger != true && col.CompareTag("enemy"))
    15.         {
    16.          
    17.             col.SendMessageUpwards("underAttack", jabDamage);
    18.         }
    19.  
    20.     }
    21.  
    22. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemyScript : MonoBehaviour {
    5.     private bool knock = false;
    6.     private float knockbackDuration = .3f;
    7.     private float knockbackTimer = 0;
    8.     public int currHealth;
    9.     public int maxHealth=30;
    10.     private Rigidbody2D enemeyRigidBody;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         enemeyRigidBody = GetComponent<Rigidbody2D>();
    15.         enemeyRigidBody.isKinematic = true;
    16.         currHealth = maxHealth;
    17.         Debug.Log(currHealth);
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update ()
    23.     {
    24.         if (knock)
    25.         {
    26.             if (knockbackTimer > 0)
    27.             {
    28.                 knockbackTimer -= Time.deltaTime;
    29.             }
    30.             else
    31.             {
    32.                 knock = false;
    33.                 enemeyRigidBody.isKinematic = true;
    34.             }
    35.         }
    36.         if (currHealth <= 0)
    37.         {
    38.  
    39.             Destroy(gameObject);
    40.         }
    41.    
    42.     }
    43.  
    44.  
    45.     public void underAttack(int damage)
    46.     {
    47.      
    48.         currHealth -= damage;
    49.         Debug.Log(currHealth);
    50.          
    51.     }
    52.     void OnTriggerEnter2D(Collider2D col)
    53.     {
    54.         if (col.CompareTag("jabbing"))
    55.         {
    56.             knock = true;
    57.             knockbackTimer = knockbackDuration;
    58.             enemeyRigidBody.isKinematic = false;
    59.             enemeyRigidBody.AddForce(Vector3.right * 100);
    60.         }
    61.      
    62.      
    63.     }
    64.  
    65. }
    66.  
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try add in Line 9:
    enemeyRigidBody = GetComponent <Rigidbody2D> ();