x


Fire Animation Help

Ok guys, I have a script attached to an empty called "SpawnPoint". The SpawnPoint is attached to my gun. I want it to play an animation when I press the required key. here is the script:

var prefabBullet:Transform; var shootforce:float;

function Update () { if(Input.GetButtonDown("Fire2")) { var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootforce); }

if(Input.GetButtonDown("Fire2"))
{
    animation.Play("Aim-FIRE!");
}

}

The firing itself works well enough, but the animation refuses to play. This is the error I get:

MissingComponentException: There is no 'Animation' attached to the "SpawnPoint" game object, but a script is trying to access it. You probably need to add a Animation to the game object "SpawnPoint". Or your script needs to check if the component is attached before using it. UnityEngine.Animation.Play (System.String animation) fire.Update () (at Assets/fire.js:15)

Is there a way to get it to access the script attached to my player?

more ▼

asked Mar 12 '11 at 11:27 PM

Sajid 1 gravatar image

Sajid 1
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

This is what I got for the script you gave me:

var prefabBullet:Transform; var shootforce:float;

function Update () { if(Input.GetButtonDown("Fire2")) { var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootforce); } }

private var player : GameObject;

function Start(){ player = GameObject.FindWithTag("Player"); }

function Update(){ if(Input.GetButtonDown("Fire2")){ player.gameObject.animation.Play("Aim-FIRE!"); } }

This is the error I got: Assets/fire.js(19,10): BCE0089: Type 'fire' already has a definition for 'Update()'.

Im sorry, but i'm a nooby at scripting.

more ▼

answered Mar 13 '11 at 05:18 PM

Sajid 1 gravatar image

Sajid 1
1 1 1 1

it's because you have 2 functions of the same name. put player.gameObject.animation.Play("Aim-FIRE!"); into your update function and put player = GameObject.FindWithTag("Player"); into your start function, you then delete the functions that are unused. hope you can understand this :P

Mar 14 '11 at 12:23 AM MonkeyAssassin8
(comments are locked)
10|3000 characters needed characters left
private var player : GameObject;

function Start(){
    player = GameObject.FindWithTag("Player");
}

function Update(){
    if(Input.GetButtonDown("Fire2")){
        player.gameObject.animation.Play("Aim-FIRE!");
    }
}
more ▼

answered Mar 13 '11 at 02:09 AM

MonkeyAssassin8 gravatar image

MonkeyAssassin8
271 35 38 43

Thanks, I'll try that.

Mar 13 '11 at 05:09 PM Sajid 1
(comments are locked)
10|3000 characters needed characters left

I think you just need to add an 'Animation' Component from the Component Menu onto the Spawnpoint GameObject. No animation component = no animation playing. ;)

more ▼

answered Mar 13 '11 at 06:23 AM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

(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:

x3922
x227
x153
x19
x6

asked: Mar 12 '11 at 11:27 PM

Seen: 2068 times

Last Updated: Mar 13 '11 at 05:31 AM