Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Aug 14, 2017 at 08:15 PM · scripting beginnerbeginnerscriptingbasicsnoob

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 :) 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 :) 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 :D Thanks in advance for this!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ledifen · Aug 14, 2017 at 10:57 PM

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 link 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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Aug 16, 2017 at 08:00 AM 0
Share

Hello again, I have followed your tips and looked in other places, and I think my script could work although it still has 1 error I have no clue how to resolve:

  private var AimedIN = false;
  var AimIN = Animation;
  var AimOUT = Animation;
  
  function Update () {
      if(Input.Get$$anonymous$$eyDown("mouse 1")) {
          if(AimedIN == false); {
          GetComponent.<Animation>().Play("AI$$anonymous$$-IN");
          this.GetComponent("GunFire").enabled=false;
          yield WaitForSeconds(0.6);
          this.GetComponent("GunFire").enabled=true;
          AimedIN = true;
          Debug.Log("Aimed In");
      }        
  }        
  }
  function Update2 () {
      if(Input.Get$$anonymous$$eyDown("mouse 1")) {
          if(AimedIN == true) {
          AimedIN = false;
          GetComponent.<Animation>().Play("AI$$anonymous$$-OUT");
          this.GetComponent("GunFire").enabled=false;
          yield WaitForSeconds(0.6);
          this.GetComponent("GunFire").enabled=true;
          Debug.Log("Aimed Out");
      }
  }
  }

The error is: Script error (GlockAI$$anonymous$$): Update() can not be a coroutine. Thank you so much for helping! And again, the solution might me really simple but I have no clue for these things :)

avatar image Ledifen $$anonymous$$ · Aug 18, 2017 at 12:25 PM 0
Share

oh, didn't see you had answered, sorry.

So, I need info to try and help you.

  1. Why do you use function Update2 ? Is there a specific reason why you don't put your two if statements in the function Update?

  2. The problem with the Coroutine stuff, I think, is that you cannot use "Yield" in update. I think it's for performance reasons, but not sure. What you should do is create another function than you name as you want and put the yield in it. Then you call this function when you need it. :

    function Update () { if(Input.Get$$anonymous$$eyDown("mouse 1")) { if(AimedIN == false); { GetComponent.().Play("AI$$anonymous$$-IN"); this.GetComponent("GunFire").enabled=false; wait (); // you call the function called "wait" this.GetComponent("GunFire").enabled=true; AimedIN = true; Debug.Log("Aimed In"); } } } function Update2 () { if(Input.Get$$anonymous$$eyDown("mouse 1")) { if(AimedIN == true) { AimedIN = false; GetComponent.().Play("AI$$anonymous$$-OUT"); this.GetComponent("GunFire").enabled=false; wait (); this.GetComponent("GunFire").enabled=true; Debug.Log("Aimed Out"); } } } function wait (){ //you create the function that uses the yield yield WaitForSeconds(0.6); }

  3. There's also a thing in C# : when you use decimals numbers, you have to add an F after it. That how I understand it. $$anonymous$$aybe hat'll be an issue and you should write "yield WaitForSeconds (0.6F);

  4. Be also careful to this : if(AimedIN == true) { AimedIN = false;. Here you set your boolean to false right after checking if it's true, so the condition AimedIN == true is not met anymore and it might give weird results. So maybe put it at the very end of your if statement, after the wait() function.

That's all I can say right now. I'll check more often if I have an answer. And don't worry about "having no clue", as you put it. You'll get there, it juste takes time. :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to increase the time between updates? 1 Answer

Beginner Help: Making a Script Public for GetComponent Call on another script 1 Answer

Basic Enemy follow script for Unity five, desperately needed! 0 Answers

Accessing script from another object 1 Answer

How to slowly rotate an object only once using scripting? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges