- Home /
call/trigger animation of other object
Hello I have such objects in the scene: collider (that acts as trigger), elevator, animated button. Now when I enter the collider area and press f button and then get into elevator it goes up. I want make it so that when i enter the collider area and press f button the animation of other object named animated button will play too. I have such code for now.
Trigger script:
static var leverswitch: int;
var trigger: int=1;
var Player: Transform;
var AnimationFile: AnimationClip;
function OnTriggerStay (other: Collider)
{
if (Input.GetKeyDown("f"))
{
leverswitch =trigger;
Player.parent =null;
animation.Play("AnimationFile");
}
}
Elevator script:
var Player: Transform;
var Elevator: Transform;
var point1: Vector3;
var point2: Vector3;
var speed: int=5;
var boolswitch: lever;
function OnTriggerStay(other: Collider) {
boolswitch=FindObjectOfType (lever);
if (boolswitch.leverswitch==1) {
if (Elevator.transform.position.y < point2.y) {
Player.parent=Elevator;
Elevator.transform.Translate(Vector3.up*Time.deltaTime*speed);
}
}
}
This does not work as I get error: MissingComponentException: There is no 'Animation' attached to the "aktywator_windy" game object, but a script is trying to access it.
Can someone tell me how to fix it (make it work)?
Maybe this ugly picture will help it understand better:
Here "animation" variable seems null.
animation.Play("AnimationFile");
Is the object attach to this script has an animation ? Enabled ?
The "AnimationFile" variable is also unused. $$anonymous$$aybe you forget something ?
var AnimationFile: AnimationClip;
"Is the object attach to this script has an animation ? Enabled ?" If you ask if button have animation then yes it has. And I think it is enabled as I dont know how to disable animation. Also button does not have any scripts at all the thing is I want to play its animation using mentioned trigger.
"Here "animation" variable seems null.
animation.Play("AnimationFile");
This "AnimationFile" is unused. $$anonymous$$aybe you forget something ?
var AnimationFile: AnimationClip;"
I am begginer in scripting but I think this is correct as it gives me nice paremeter in Inspector where I can choose any animation from project assets. If im wrong then correct me.
Your script file need to be on the object with the animation. Or you have to use an other Animation variable.
You can click on the error and see the call stack. Something like this :
Animation.Start () (at Assets/Animation.cs:8)
This will help you to find your problem.
I did try to click on that and read logs and sadly it gave me no idea or tips how to resolve that. Could you be so nice to tell me that other animation variable then?
It give you the file and the line of your execution error. It's vital for debugging to know these information.
var Animation: Anim; //Drag and drop in your editor the animation.
function OnTriggerStay (other: Collider){
//...
Anim.Play();
}
Answer by Mander · Aug 20, 2012 at 05:18 PM
where is ur script attached? and where is ur animation.? in that script u r tryin to acces an animation in the object where the script is attached. so u might need to call the correct gameObject to play the animation. `gameObject.FindWithTag("player");` or u can make a variable `var animated : GameObject;` and add ur player in the inspector anyway u want will work.
but remember that the object must contain the animation component with its animation.
srry for the confusing bad english. xD
the scripts are attached to empty's with box colliders as triggers
animation is on imported object from blender. Yes i know i need to call correct gameObject but i have no idea how. I will try to remade it as you written.
Remade the code to this yet still it does not work
static var leverswitch: int; var trigger: int=1; var Player: Transform; var animated : GameObject;
function OnTriggerStay (other: Collider) { if (Input.Get$$anonymous$$eyDown("f")) { leverswitch =trigger; Player.parent =null; // niszczy dziedziczenie animation.Play("animated"); } }
As GameObject i did choose button (it has its animation) from assest
in any script u can find objects by name or tags like this
var objectAnim : GameObject;
this way u can assign it in the inspector. just drag the gameObject.
then use like this objectAnim.animation.Play("AnimationFile");
or u can find in the script
var objectAnim : GameObject;
objectAnim = gameObject.Find("gameObject");
and use it just like in the 1st example
Ok I do not get any errors now but still it wont animate i must do something wrong? Also i would vote you up for helping me but somehow i cant (no premission error) sorry
Answer by mantisghost · Aug 20, 2012 at 06:45 PM
Ok problem solved thanks again the correct code goes like this for me
static var leverswitch: int;
var trigger: int=1;
var Player: Transform;
//var AnimationFile: AnimationClip;
var animated : GameObject;
function OnTriggerStay (other: Collider)
{
if (Input.GetKeyDown("f"))
{
leverswitch =trigger;
Player.parent =null;
animated.animation.Play("dzwignia");
}
}
Everything works fine now
the thing is I can not i get "You don't have permission to do this action. Please Login as another user"
i see u dont have enough karma i guess. thx anyways and happy coding :P
Your answer
Follow this Question
Related Questions
Animated Object Problem 1 Answer
Presure plate that triggers an animation 1 Answer
Retrigger animation after play through 0 Answers
object animation trigger 0 Answers
importing exporting Animation help. 1 Answer