I have run into the error code:
“NullReferenceException: Object reference not set to an instance of an object
StarterAssets.ThirdPersonController.YesIsAttacking () (at Assets/Scripts/ThirdPersonController.cs:37)”
This happens whenever I try to run the Method “Damage()” (this is within the “TrainingDummy” script) from another script “ThirdPersonController”.
public GameObject objectTD;
public void YesIsAttacking()
{
_animator.SetBool("IsAttacking", true);
// set triggerStay to true if the katana is already in the Training Dummy hitbox or false if not
triggerStay = GameObject.Find("katana").GetComponent<Sword>().IsTriggerStay();
if (triggerStay == true)
{
// assign ObjectTD to the training_dummy in the scene
objectTD = GameObject.Find("training_dummy");
}
}
public void NotIsAttacking()
{
_animator.SetBool("IsAttacking", false);
}
I have looked online and double and triple checked that all the code I am using is exactly as it should be. I have even used the exact same code in another script in the same project to run my “HitSound()” method (this is within the “Sword” script) from the “TrainingDummy” script and it works perfectly.
public void Damage()
{
GameObject.Find("katana").GetComponent<Sword>().HitSound();
health++;
Debug.Log("Enemy Health: " + health);
enemAnim.SetTrigger("Hit");
}
I can get the “training_dummy” as a GameObject in the “ThirdPersonController” script. I only get the error when I then use that GameObject to get “TrainingDummyScript” and run the “Damage()” method. When I ran the code shown below I got back “No Training Dummy” in the console.
public GameObject objectTD;
public void YesIsAttacking()
{
_animator.SetBool("IsAttacking", true);
// set triggerStay to true if the katana is already in the Training Dummy hitbox or false if not
triggerStay = GameObject.Find("katana").GetComponent<Sword>().IsTriggerStay();
if (triggerStay == true)
{
// assign ObjectTD to the training_dummy in the scene
objectTD = GameObject.Find("training_dummy");
// if the script can't find the TrainingDummy Script then return "No Training Dummy" in log
if (objectTD.GetComponent<TrainingDummy>() == null)
{
Debug.Log("No Training Dummy");
}
// if the script can find TrainingDummy script then run Damage() function in that script
else
{
objectTD.GetComponent<TrainingDummy>().Damage();
}
}
}
public void NotIsAttacking()
{
_animator.SetBool("IsAttacking", false);
}
I’ve attached images of the error code I got as well as the Hierarchy, and the inspectors of the sword, player, and training dummy.
I am sure I could code a work around to solve the problem I am trying to fix in another way, but being able to run a method from another script is so fundamental I want to figure out why it’s not working now for future reference.




