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();