Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 CPT.Lutchmedial · Jul 21, 2015 at 07:39 AM · javascriptspriteupdate

Only run the function once

I have a script (See Below)

 #pragma strict
 
  var SpawnArray : GameObject[];
  var roundedTime : int = 0;
  var difficulty : float = 1.0;
  var gameTime : float = 0.0;
  var difficultyMod : int = 20;
  var lowBlocks : GameObject[];
 function Start () 
 {
 }
 
 function Update () 
 {
     if(roundedTime % 5 == 0)
     {
         SpawnNewOBJ();
     }
     gameTime += Time.deltaTime * difficulty;
     roundedTime = Mathf.Round(gameTime);
     if(gameTime > difficultyMod)
     {
         difficulty += 0.5f;
         difficultyMod *= 2;
     }
 }
 function SpawnNewOBJ()
 {        
         var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
         var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
         var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
 }

What I am trying to do is when the rounded time is a divisible of 5, it spawns 1 object. But what is happening is it runs the function"SpawnNewOBJ" a million times because the rounded time stays divisible by 5 for 1 second. May be hard to understand sorry. How would I make it so the function only gets called once?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BiG · Jul 21, 2015 at 07:42 AM

The simplest thing I can think about is using a lock.

declare it as an exposed variable:

 var lock : boolean = false;

Set as true when you're spawning:

 function SpawnNewOBJ(){        
     lock = true;
     //The rest of your code as is...
 }

Remove it when new reassignment takes place:

 roundedTime = Mathf.Round(gameTime);
 lock = false;

Add it as a condition for the function's call:

 if(roundedTime % 5 == 0 && !lock)

Hope this helps.

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 Veldars · Jul 21, 2015 at 07:47 AM 0
Share

I think your solution while pose the same problem because you unlock your lock in the update function. because of the "Round" function assign the variable doesn't mean change his value.

(I haven't try it's just a guess)

avatar image
0

Answer by Veldars · Jul 21, 2015 at 07:45 AM

For me the easiest way to do that is simply to make something like that :

      if(roundedTime % 6 == 0)
      {
          SpawnNewOBJ();
          gameTime += 1;
      }

Or to retake the previous answer :

 var lock : bool = false;
 var prevTime : int = 0;
 
      function Update () 
      {
          if(roundedTime % 5 == 0 && !lock)
          {
              lock = true;
              SpawnNewOBJ();
          }
          prevTime = roundedTime ;
          gameTime += Time.deltaTime * difficulty;
          roundedTime = Mathf.Round(gameTime);
          if (prevTime  != roundedTime )
              lock  = false;
          if(gameTime > difficultyMod)
          {
              difficulty += 0.5f;
              difficultyMod *= 2;
          }
      }
      function SpawnNewOBJ()
      {        
              var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
              var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
              var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
      }
 

Hope this help...

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 Lutchmedial · Jul 21, 2015 at 07:52 PM 0
Share

What if I made a timer and when its zero, I spawn an object. At the end of the spawn function, it resets the timer.

@Veldars ps. thanks for the reply

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Not updating when script value is changed by an editor script 1 Answer

Variables in GUI not updating/changing? (javascript) 1 Answer

Can't Get 2D Sprite to Animate 0 Answers

Getting updated variables from another script 2 Answers

Script is causing immense lag, and I don't know what's causing it. 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