Need Help with a animation play script.

Hey Guys , I made an animation script which play an animation in a different object from the one it is in.I was working well , but after I added the sound part of the script , it just stopped working at all! Please help me , here you go the script.

var target : GameObject;
var myVar = false;
var triggered = false;
var Trigger1 : AudioClip;

function OnTriggerEnter(other : Collider)
{
myVar = true;
}

function OnTriggerExit(other : Collider)
{
myVar = false;
}

 
function Update()
{
if(Input.GetKeyDown(KeyCode.En) && !myVar)//if yourVar is true
{
    audio.PlayOneShot(Trigger1);
    triggered = true;
    target.animation.Play("keyAnim");
    myVar = false;
    
}
}

Here is the changes I have made:

public var target : GameObject;                    //CHANGED THIS LINE
var myVar = false;
var triggered = false;
                                                   //REMOVED THIS LINE

function OnTriggerEnter(other : Collider)
{
myVar = true;
}
 
function OnTriggerExit(other : Collider)
{
myVar = false;
}
 
 
function Update()
{
if(Input.GetKeyDown(KeyCode.E) && myVar == true) //CHANGED THIS LINE
{
    audio.Play();                                //CHANGED THIS LINE
    triggered = true;
    target.animation.Play("Move");
    myVar = false;
 
}
}

Make sure to also do the following:

  • Add an Audio Source component to the object the script is attached to. (If you are having the sound come from a different object, make a reference to that object.
  • add the GameObject “target” to the script via the Inspector.

haven’t tried but this is well logic

var target : GameObject;
var myVar : boolean = false;
var triggered : boolean = false;
 
function OnTriggerEnter(other : Collider)
{
myVar = true;
}
 
function OnTriggerExit(other : Collider)
{
myVar = false;
}
 
 
function Update()
{
if(Input.GetKeyDown(KeyCode.E) && myVar)
{
audio.Play();
triggered = true;
target.animation.Play("keyAnim");
myVar = false;
 
}
}

//Trigger has no fuction yet!!

//Copy this if you want the trigger have function in function update
if(triggered){
   //do something
}