I need help with my scripts!

Hello, I am new to scripting and wanted to make a simple script for my game. I have the idea written down but since I don’t really know how it works I really need someone to fix it for me :slight_smile:
Sorry if this is really simple, but again I am new to this and instead of completely copying from others I want to learn how to fix it so I can make other working ones :slight_smile:
Here’s the script, it’s in javascript:

var Flash : GameObject;
private static var AimedIN;

function Update () {
	if(Input.GetButtonDown("AIM")) {
		GetComponent.<Animation>().Play("AIM-IN");
		AimedIN = true;
	}
}

if AimedIN = true {
	if(Input.GetButtonDown("AIM")) {
		GetComponent.<Animation>().Play("AIM-OUT");
		AimedIN = false
	}
}

I think it’s easy to understand what I’m trying to do, but I just want a working version :smiley:
Thanks in advance for this!

Hello

I’m not experienced with JS at all, but here’s a few things that might help you if I’m not mistaken. C# and JS have some similar ways to go about it so this might help (before you change things in you script, read everything. I’ve written my process so the beginning might not be the solution I think is best):

  1. I don’t think you can put an If statement outside of a function. Maybe try to put it in the update function with the rest.

  2. I also think there’s a problem with your “GetComponent blahblah”. For all that I know (and it’s not much, so it’s maybe wrong), You should get your component in an Awake function and have a var that link to them… Errr. Sorry for the vocabulary ^^. I mean, you should write "private var aimInAnim : Animation;" at the top of your code and do the same for the second animation (of course changing the name “aimInAnim”). That way you declare them properly.

  3. Then in the function Awake (), you do something like “aimInAnim = GetComponent(Animation);” The problem here, is that you’re going to have the exact same two “GetComponent” for two different var, which is bad. So th solution I can provide is maybe a little complicated.

  4. You have to deal with The animator instead of the animation. Here a 1 for my own question that I answered myself that explains how I did it.

  5. You will basically use two animations linked to Any State and create 2 parameters (booleans), one for each animation. Each animation has one condition ===> their assigned boolean is true. And then in you code, use the instructions I provided.

  6. Of course if you follow that solution, you have to declare an animator and not the animations and then enabled it in a start function. You also have to set the boolean parameter to false, and only set it to true when you need the animation. Something like that :

    public var animatorName : Animator;
    private static var AimedIN : boolean = false; // I think that you want it to be false when the game starts…

     function Start () {
             animatorName.enabled = true;
             animatorName.SetBool("boolean1Name", false);
             animatorName.SetBool("boolean2Name", false);
    }
    
    function Update ()  {
         if(Input.GetButtonDown("AIM")) {
             animatorName.SetBool("boolean1Name", true);
             AimedIN = true;
         }
     
          if AimedIN = true {
             if(Input.GetButtonDown("AIM")) {
                animatorName.SetBool("boolean2Name", true);
                AimedIN = false
             }
         }
     }
    

I’m telling you… I really do not know if what I’m saying is right, especially since I work with C#…
I tried anyway.

If you have any question about the animator, ask away, I might (or might not ^^') be able to answer.

Tell me if this helps you in any way.