x


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: alt text

simplify.png (243.5 kB)
more ▼

asked Aug 18 '12 at 07:55 PM

mantisghost gravatar image

mantisghost
0 1 2 2

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. Maybe you forget something ?

var AnimationFile: AnimationClip;
Aug 20 '12 at 04:52 PM watermy

"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. Maybe 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.

Aug 20 '12 at 05:02 PM mantisghost

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.

Aug 20 '12 at 05:15 PM watermy

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?

Aug 20 '12 at 05:16 PM mantisghost

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();
}
Aug 20 '12 at 05:21 PM watermy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Aug 20 '12 at 05:18 PM

Mander gravatar image

Mander
473 2 5

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.GetKeyDown("f")) { leverswitch =trigger; Player.parent =null; // niszczy dziedziczenie animation.Play("animated"); } }

As GameObject i did choose button (it has its animation) from assest

Aug 20 '12 at 05:21 PM mantisghost

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

Aug 20 '12 at 05:29 PM Mander

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

Aug 20 '12 at 05:39 PM mantisghost
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 20 '12 at 06:45 PM

mantisghost gravatar image

mantisghost
0 1 2 2

upvote :)

Aug 20 '12 at 07:19 PM Mander

the thing is I can not i get "You don't have permission to do this action. Please Login as another user"

Aug 20 '12 at 09:09 PM mantisghost

i see u dont have enough karma i guess. thx anyways and happy coding :P

Aug 20 '12 at 09:32 PM Mander
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3807
x1095
x986
x76

asked: Aug 18 '12 at 07:55 PM

Seen: 969 times

Last Updated: Aug 20 '12 at 09:32 PM