Calling from another script, for a muzzleflash script.

So the AI i am using does not have a muzzle flash when it shoots.I have another script that will make a muzzleflash when the AI attacks but i dont understand what it is telling me to do when it says “Call OnShooting() from somewhere in your firing script” in the comments.

This is the Muzzle Flash script.

var muzzleFlash : Renderer; //Your emitter prefab
var gunEnd : Transform; //An empty gameobject positioned at the end of the gun as a child
var gunEnd2 : Transform; //Same as above but the other one

//Call OnShooting() from somewhere in your firing script
function OnShooting () {
    Instantiate(muzzleFlash, gunEnd.position, gunEnd.rotation);
    yield WaitForSeconds (0.2);
    Instantiate(muzzleFlash, gunEnd2.position, gunEnd2.rotation);
}

This is the part of the AI script that is the attack, i believe.

public void Attack (Vector3 targetDirectiom)
    {
        if (Time.time > attackTemp + AttackDelay) {
            Vector3[] dirs = new Vector3[1];
            dirs [0] = targetDirectiom.normalized;
            character.DoDamage (this.transform.position + character.DamageOffset, dirs, character.Damage, character.DamageLength, character.Penetrate,"",character.Team);
            character.AttackAnimation ();
            attackTemp = Time.time;   
        }
    }

Im not sure what call onShooting means.

within the Attack function add

GetComponent<MuzzleFlash>().OnShooting();

assuming the muzzleflash script is on the same game object. You “call” a function by “executing” it.

i.e. your existing Attack function “calls” DoDamage(…) function from the character component.

I see what your saying, let me try this. Thank you!

AICharacterController.cs(41,16): error CS0246: The type or namespace name `MuzzleFlash’ could not be found. Are you missing a using directive or an assembly reference?

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterSystem))]

public class AICharacterController : MonoBehaviour
{
    public string PlayerTag = "Player";
    public bool UseMassiveController;
    public string[] TargetTag = {"Player"};
    public GameObject ObjectTarget;
    [HideInInspector]
    public Vector3 PositionTarget;
    [HideInInspector]
    public CharacterSystem character;
    [HideInInspector]
    public float DistanceAttack = 2;
    public float DistanceMoveTo = 20;
    public float TurnSpeed = 10.0f;
    public bool BrutalMode;
    public bool RushMode;
    public float PatrolRange = 10;
    [HideInInspector]
    public Vector3 positionTemp;
    [HideInInspector]
    public int aiTime = 0;
    [HideInInspector]
    public int aiState = 0;
    private float attackTemp = 0;
    public float AttackDelay = 0.5f;
    public float LifeTime = 30;
    public float IdleSoundDelay = 10;
    float lifeTimeTemp = 0;
    float jumpTemp, jumpTime, soundTime,soundTimeDuration;
    public int JumpRate = 20;
    public MuzzleFlash muzzleFlash;

    void Start ()
    {
        character = gameObject.GetComponent<CharacterSystem> ();
        positionTemp = this.transform.position;
        aiState = 0;
        attackTemp = Time.time;
        lifeTimeTemp = Time.time;
        jumpTemp = Time.time;
        soundTime = Time.time;
        soundTimeDuration = Random.Range(0,IdleSoundDelay);
    }
   
    public void Attack (Vector3 targetDirectiom)
    {
        if (Time.time > attackTemp + AttackDelay) {
            Vector3[] dirs = new Vector3[1];
            dirs [0] = targetDirectiom.normalized;
            character.DoDamage (this.transform.position + character.DamageOffset, dirs, character.Damage, character.DamageLength, character.Penetrate,"",character.Team);
            character.AttackAnimation ();
            attackTemp = Time.time; 
            GetComponent<MuzzleFlash>().OnShooting();
        }
    }

not sure what is going on, im sure i did it wrong. both scripts are on the same game object

Sounds like something isn’t spelt correctly, capitalisation matters here; is the muzzle flash script

 public class MuzzleFlash : Monobehaviour {...

or

 public class Muzzleflash : Monobehaviour {...

or something else?

nvm i found another way, the way above i realized didnt need to be two seperate scripts, and it also wasnt destroying the muzzleflash, so my AI was leaving a trail.

so i did some searching and learned you cant destory transform objects or game objects to prevent data loss?

so this is what i came up with and its working fine. the players muzzleflash was fine, this is for the AI and the original author of the AI script didnt add muzzleflash options.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterSystem))]

public class AICharacterController : MonoBehaviour
{
    public string PlayerTag = "Player";
    public bool UseMassiveController;
    public string[] TargetTag = {"Player"};
    public GameObject ObjectTarget;
    [HideInInspector]
    public Vector3 PositionTarget;
    [HideInInspector]
    public CharacterSystem character;
    [HideInInspector]
    public float DistanceAttack = 2;
    public float DistanceMoveTo = 20;
    public float TurnSpeed = 10.0f;
    public bool BrutalMode;
    public bool RushMode;
    public float PatrolRange = 10;
    [HideInInspector]
    public Vector3 positionTemp;
    [HideInInspector]
    public int aiTime = 0;
    [HideInInspector]
    public int aiState = 0;
    private float attackTemp = 0;
    public float AttackDelay = 0.5f;
    public float LifeTime = 30;
    public float IdleSoundDelay = 10;
    float lifeTimeTemp = 0;
    float jumpTemp, jumpTime, soundTime,soundTimeDuration;
    public int JumpRate = 20;
    public GameObject original;
    public Transform gunEnd; //An empty gameobject positioned at the end of the gun as a child
    public Transform gunEnd2; //Same as above but the other one


    void Start ()
    {
        character = gameObject.GetComponent<CharacterSystem> ();
        positionTemp = this.transform.position;
        aiState = 0;
        attackTemp = Time.time;
        lifeTimeTemp = Time.time;
        jumpTemp = Time.time;
        soundTime = Time.time;
        soundTimeDuration = Random.Range(0,IdleSoundDelay);
    }
 
    public void Attack (Vector3 targetDirectiom)
    {
        if (Time.time > attackTemp + AttackDelay) {
            Vector3[] dirs = new Vector3[1];
            dirs [0] = targetDirectiom.normalized;
            character.DoDamage (this.transform.position + character.DamageOffset, dirs, character.Damage, character.DamageLength, character.Penetrate,"",character.Team);
            character.AttackAnimation ();
            attackTemp = Time.time;
            GameObject clone = (GameObject)Instantiate (original, gunEnd.position, gunEnd.rotation);
            Destroy (clone, 1.0f);

        }
    }

idk what was wrong. something about not being able to call from the directive or something. but all is well now. thanks for teaching me about calling functions.