• 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
Question by Americus · Jul 09, 2012 at 10:03 AM · variableupdatefunction

Having code fire once in update function?

Hi, I'm a Unity noob (still learning) and I'm trying to build a basic space invaders clone to get my head around how everything works. Here is my enemy movement script, it's working for left and right movement, but when I fire the jump variable, they either fly off the screen (because it keeps firing the jump code) or doesn't jump at all (if I turn the variable off after it fires, the entire variable never even fires once).

I've tried Invoke instead, and placed the jump code in a function, but then I couldn't work out how to have it affect all of the enemies (as it would just work on the one that triggered the jump scenario). I was playing with GameObject.FindGameObjectsWithTag (since all my enemies are tagged "Enemy") but couldn't work out how to then take that information and use it to move all the enemies down (sigh).

I've searched everywhere, so I'm hoping somebody can answer this simple question for me?

    function Update () {
 
  var amtToMove = enemySpeed * Time.deltaTime; 
  
  if (startMove==true)
  {
  transform.Translate(Vector3.forward*amtToMove);
  }
  
  if (transform.position.z >= 63)
  {
  startMove = false;
  JumpOn=true; 
  hitRIGHT = true;
  }
  
  if(transform.position.z <= -61)
  {
  hitRIGHT = false;
  JumpOn=true; 
  hitLEFT = true; 
  }
  
  if(hitRIGHT==true)
  {
  hitLEFT=false;
  transform.Translate(Vector3.back*amtToMove);
  JumpOn=false;
  }
  
  if(hitLEFT==true)
  {
  hitRIGHT=false;
  transform.Translate(Vector3.forward*amtToMove);
  JumpOn=false;
  }
  
  if (JumpOn==true)
  {
  transform.position.y -=20;
  }

Comment
Linus

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Linus · Jul 09, 2012 at 10:55 AM

 if (JumpOn==true)
  {
  transform.position.y -=20;
  JumpOn = false;
  }

 

Also make sure that the conditions for jumping are not true again for the next frame.

Comment
Bunny83
animusrecro

People who like this

2 Show 9 · 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 Americus · Jul 09, 2012 at 11:01 AM 0
Share

Sorry, I don't think I was clear. This is for the automatic movement of the enemy. I can't have the enemy movement linked to player control. The 'jump' is when the enemies in space invaders hit the side of the screen, and move down.

avatar image Linus · Jul 09, 2012 at 11:04 AM 0
Share

oops, you where not unclear, I was just not paying attention. I will update or remove my answer

avatar image Americus · Jul 09, 2012 at 11:18 AM 0
Share

All good! I actually tried the updated code, and it refused to work. It results in the enemies not jumping at all, strangely enough. I then got the impression you couldn't turn a variable to false if you were doing it within the variable itself, which is why the code became the way it is.

avatar image Bunny83 · Jul 09, 2012 at 11:22 AM 0
Share

Hmm, i thought the revision history was fixed... it seems not... I still just see the last revision.

avatar image Linus · Jul 09, 2012 at 11:52 AM 1
Share

Have a look at http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html

for(var thisEnemy : GameObject in GameObject.FindGameObjectsWithTag("Enemy")) { thisEnemy.transform.position.y -=20; thisEnemy.transform.position.y = JumpOn = false; }

Show more comments
avatar image

Answer by darkironphoenix · Jul 09, 2012 at 01:03 PM

You could fire a function from within your update if's.

 if (JumpOn==true)
  {
  JumpFunction();
  }

And then have the code in the function.

 function JumpFunction(){
 transform.position.y -=20;
  JumpOn = false;
 }
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

summing up items price in runtime 1 Answer

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 1 Answer

Variable Doesn't Update in "Update" function 0 Answers

Update() doesnt call method or modify values 1 Answer

Variables in base class not changing, sorta...?? 0 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