Search Unity

Question Object reference not set to instance of an object

Discussion in 'Animation' started by zeemyname, Aug 8, 2022.

  1. zeemyname

    zeemyname

    Joined:
    Apr 5, 2022
    Posts:
    5
    I am creating a prototype i set animator controller.

    when i press "s" "Bool get true" it plays animation at the end of animation an event is called to make bool false.
    upload_2022-8-8_19-6-27.png
    But when i press "S" second time it gives Object reference not set to an instance of object and animation does not get played. I know the error is very basic but i am still getting trouble to clear it.
    Following are my code scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DandaAnimations : MonoBehaviour
    6. {
    7.     Animator animator;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         animator = this.GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.     public void HitDown()
    20.     {
    21.         animator.SetBool("hitDown", true);
    22.     }
    23.     public void Strike()
    24.     {
    25.         animator.SetBool("dandaSway", true);
    26.     }
    27.     public void TurnDownFalse()
    28.     {
    29.         animator.SetBool("hitDown", false);
    30.     }
    31.     public void TurnStrikeFalse()
    32.     {
    33.         animator.SetBool("dandaSway", false);
    34.     }
    35. }
    36.  
    Controller Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DandaControler : DandaAnimations
    6. {
    7.     DandaAnimations dandaAnimations;
    8.     DandaAnimations currentAction;
    9.     GameObject danda;
    10.     Animator d_animator;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         d_animator = GetComponent<Animator>();
    15.         dandaAnimations = this.gameObject.GetComponent<DandaAnimations>();
    16.     }
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         PullGulliUp();
    21.     }
    22.     void PullGulliUp()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.S))
    25.         {
    26.             dandaAnimations.HitDown();
    27.         }
    28.         if (Input.GetKeyUp(KeyCode.S))
    29.         {
    30.             dandaAnimations.TurnDownFalse();
    31.         }
    32.     }
    33. }
    34.