Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ToxicNova · Feb 22, 2015 at 07:07 PM · animationjavascriptvariableerror message

How to change a variable in the same script?

Hello, so I have this script attached to a gun my first person controller has. What it does is it plays an animation that zooms in and out the gun when the right mouse button is pressed. What i need it to do is when I first press the right mouse button, it plays the zoom animation on my gun and creates a variable called counter which has a int. value set to 1. Next, I need an if statement under function update that tests for is counter is = to 1. Then it tests if the right mouse button is clicked. If it is true, it will play a gun zoom out animation and reset counter to 0. I have a script below but I get the error, their is already a local variable, counter. The script is bellow in java script.

 #pragma strict
 
 var Gun : Transform;
 
 function Update () {
     if (Input.GetButtonDown("Fire2")) 
     {
         var Counter : int = 1;
         Gun.animation.Play("Gun Zoom");
     }
     if (Counter == 1)
     {
         if (Input.GetButtonDown("Fire2"))
         {
             var Counter : int = 0;
             Gun.animation.Play("Gun Zoom Out");
         }
     }            
         
 }

Thanks in advance

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by karljj1 · Feb 22, 2015 at 07:11 PM

Your counter variable is hidden inside of the if statements scope. You need to define it outside and since its going to be used over more than one frame it should really have class scope.

  #pragma strict
 
  var Counter : int = 0;
  
  var Gun : Transform;
  
  function Update () {
      if (Input.GetButtonDown("Fire2")) 
      {
          Counter = 1;
          Gun.animation.Play("Gun Zoom");
      }
      if (Counter == 1)
      {
          if (Input.GetButtonDown("Fire2"))
          {
              Counter = 0;
              Gun.animation.Play("Gun Zoom Out");
          }
      }           
          
  }
Comment
Add comment · Show 5 · 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 Glurth · Feb 22, 2015 at 07:18 PM 0
Share

you are still re-declaring counter (or is that not an issue in js?)

avatar image karljj1 · Feb 22, 2015 at 07:20 PM 0
Share

Oops yes. I copied the original code and missed the var :) Ill change it

avatar image ToxicNova · Feb 22, 2015 at 07:41 PM 0
Share

When I'm using your script or Reder13's, it plays "gun zoom out" first and doesn't change counter but stays 0 all the time. On the other hand, the error isn't popping up anymore.

avatar image karljj1 · Feb 22, 2015 at 07:58 PM 2
Share

Thats because both if statements will be true.

This may work better for you:

   if (Input.GetButtonDown("Fire2")) // Zoom in when the player presses the button
   {
       Gun.animation.Play("Gun Zoom");
   }

   if (Input.GetButtonUp("Fire2")) // Zoom out when they release the button
   {
       Gun.animation.Play("Gun Zoom Out");
   }
   
avatar image ToxicNova · Feb 22, 2015 at 09:11 PM 0
Share

Lol, that was actually what I was trying to achieve but i didn't know how to do that. Thanks.

avatar image
0

Answer by Reder13 · Feb 22, 2015 at 07:13 PM

so something like this...

  #pragma strict
 
 var Gun : Transform;
 
 var Counter : int = 0;
 
 function Update () {
 
 if (Input.GetButtonDown("Fire2")){
 
  Counter = 1;
 
 Gun.animation.Play("Gun Zoom");
 
 }
 
 if (Counter == 1)
 
 {
 
 if (Input.GetButtonDown("Fire2"))
 
 {
 
 Counter = 0;
 
 Gun.animation.Play("Gun Zoom Out");
 
 }
 
 }
 
 }

Comment
Add comment · Show 1 · 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 Glurth · Feb 22, 2015 at 07:15 PM 0
Share

looks better.

avatar image
0

Answer by Glurth · Feb 22, 2015 at 07:14 PM

I'm no JS programmer, but I suspect the error has to do with the fact that the VAR statment means: allocate and use this variable name. the INT part says what kind of variable it is. This is called a variable "declaration"

You probably want to declare the variable in the class, or script, OUTSIDE of your update function. This way, it will retain it's assigned value between calls to update. (google the term "scope" for details)

Inside update, do not use the syntax to declare the variable, just use the syntax to assign a value to the (already declared) variable. probably something like Counter=0;

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make a character accelerate and play an animation when the click left-shift 1 Answer

Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer

variable has not been assigned? 1 Answer

Why can't I make this VAR public ? (Solved) 3 Answers

Javascript Movement Script. Why am I getting these errors? 2 Answers


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