C# - calling a function from another class

I have two classes where in the first class for my weapons i check for mouse click to fire the gun. I have a animations class where i checked for the same mouse click for firing to play an animation this worked fine.

Instead, in the weapons class i would like to just call the function to play the animation instead of checking for mouse click in both classes. So, i made a couple of functions in the animations class and move the code in there and tried to use broadcast message. However, i get an error when i hit fire.

BroadcastMessage FireAnimation has no receiver!
UnityEngine.Component:BroadcastMessage(String)
RayShootAK47:Update() (at Assets/Scripts/RayShootAK47.cs:164)

Here is part of the first class…

public class RayShootAK47 : MonoBehaviour {
void Update () {
		

		if(Input.GetButtonDown("Fire1")){
			if(bulletsLeft > 0)
		StartCoroutine("fireBullet");
			BroadcastMessage("FireAnimation");
}
}

Here is the animations class

using UnityEngine;
using System.Collections;

public class AK47_animations : RayShootAK47 {

	
	public Animation reload;
	public Animation zoom_in;
	public Animation zoom_out;
	public Animation show;
	public Animation hide;
	public Animation single_shot;
	public Animation triple_shot;
	public Animation multiple_shot;
	public Animation idle;
	public Animation walk;
	public Animation run;
	public Animation jump;
	public Animation zoom_single_shot;
	public Animation zoom_multiple_shot;
	public Animation zoom_triple_shot;
	
	bool isplaying = false;
	bool aim = false;
	
	
	void Start () {
	
	}
	
	
	void Update () {
	
	
	}
	
	public void ZoomInAnimation(){
		
		aim = true;
		zoom_in.animation.Play("Zoom_In");
		
	}
	
	public void ZoomOutAnimation(){
		
		aim = false;
		zoom_out.animation.Play("Zoom_Out");
	}
	
	
	public void FireAnimation(){
		
		if(!aim){
			single_shot.animation.Play("Single_Shot");
			}
			else{
				zoom_single_shot.Play("Z_Single_Shot");
			}
		
		
	}
	
	public void ReloadAnimation(){
		
		reload.Play("Reload");
	}
	
	
}

Keep in mind this worked when i checked for key presses or mouse press in both classes and not inheriting from the first class.

What should i do?
Thank you

FIXED:

okay, i got it working. Not how i wanted it to but it works. This is what i did incase someone wonders in the future.

public class RayShootAK47 : MonoBehaviour {
	
	
	 AK47_animations anim ;


        Void Start () {
		
	   anim = (AK47_animations)FindObjectOfType(typeof(AK47_animations));

       void Update () {
      if(Input.GetButtonDown("Fire1")){
			if(bulletsLeft > 0)
		StartCoroutine("fireBullet");
			
			
			anim.FireAnimation();

You have some nice code here, and are just a bit off.

First thing, is that you probably don’t want inheritance here. I love inheritance in Unity, but here it just is confusing and not what you want. You want to separate classes and two instances. Both are attached to your GO. So, for starters, make the AK47_animations extend MonoBehavior:

public class AK47_animations : MonoBehavior {

Now, again make sure you have both behaviors on your GO. Now, your BroadcastMessage should work!

There are many other ways to call methods from other scripts. One of the simplest is to put a reference to your AK47_animations in your RayShootAK47 class and use that variable to call it directly. Other options would be to do a GetComponent(), or a delegate in the animations class and have the shooter subscribe. The idea of these other methods is really only worth the complexity if you are handling lots and lots of instances or frequency of calls. In your case the BroadcastMessage() or SendMessage() is fine.

public class RayShootAK47 : MonoBehaviour {

void Update () {





       if(Input.GetButtonDown("Fire1")){

         if(bulletsLeft > 0)

       StartCoroutine("fireBullet");

         BroadcastMessage("FireAnimation");

Add to end " } } "

public class RayShootAK47 : MonoBehaviour {
void Update () {

       if(Input.GetButtonDown("Fire1")){
         if(bulletsLeft > 0)
       StartCoroutine("fireBullet");
         BroadcastMessage("FireAnimation"); 
}
}

If you are using the C# Extended messenger script, then you need to set up “listeners” on the scripts that receive messages.

Use an OnEnable and OnDisable to set them up. Follow the directions, but make sure that the functions called match the functions mentioned and make sure you have the right number of data types in the call and the receiving functions.