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 RandomUser123 · Aug 19, 2014 at 11:52 PM · updatetimerchangeswap

Changing 2 objects position over time

I have the following code which I call in my Update() function:

 void changeCapsulePosition()
     {
         int timer;
         timer = (int)Time.time;

         if(GUICounters.score >= 1)
         {
             Debug.Log(timer);
             if(timer % 5 == 0 && hasChanged == false)
             {
 
                 Vector3 tempPosition = capsule1.transform.position;
                 capsule1.transform.position = capsule2.transform.position;
                 capsule2.transform.position = tempPosition;
                 hasChanged = true;
 
             }
             hasChanged = false;
         }
 
     }

I would like my 2 objects to change position once after every 5 seconds but as it is, it switches back and forth the entire time the timer is on 5. I'm assuming this is because it is being called by Update and is ran quite a few times and since I change the hasChanged variable back to false, the condition will be true throughout the entire second.

Is there a way I can just get it to run once every 5 seconds?

Comment
Add comment · Show 2
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 robertbu · Aug 20, 2014 at 12:30 AM 0
Share

Get rid of the timer code and use InvokeRepeating() to call this function once every 5 seconds.

avatar image Myth · Aug 20, 2014 at 12:36 AM 0
Share

whenever hasChanged is set to true, it is immediately set to false again... you may want to look into that

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RandomUser123 · Aug 20, 2014 at 04:50 PM

Cheers @robertbu I set up an InvokeRepeating function which makes it behave accordingly. Seems so simple when its laid out now. I set it up to choose a random time between 1 and 5.

Thanks again, new code for anyone wondering:

     void Start () 
     {
         InvokeRepeating("changeCapsulePosition", 2, Random.Range (0,5));
     }
 
     // Update is called once per frame
     void Update () 
     {
 
     }
 
     
     void changeCapsulePosition()
     {
         if(GUICounters.score >= 1)
         {
             Vector3 tempPosition = capsule1.transform.position;
             capsule1.transform.position = capsule2.transform.position;
             capsule2.transform.position = tempPosition;
         }
 
     }
Comment
Add comment · Show 3 · 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 JayFitz91 · Aug 20, 2014 at 07:01 PM 1
Share

Great that you got it working, just a small bit of input, the Random.Range(0,5) will only pick a random number at the beginning of the scene as it is in the Start function. This number will then be the number used throughout the entire Invoke, it will never change. Just a heads up :)

avatar image robertbu · Aug 20, 2014 at 08:06 PM 1
Share

If you want to change ti$$anonymous$$g every cycle, a coroutine is a better solution. A bit of untested code to demonstrate:

 IEnumerator Start () 
 {
     yield return new WaitForSeconds(2.0f);

     while (true) {
         if(GUICounters.score >= 1)
         {
             Vector3 tempPosition = capsule1.transform.position;
             capsule1.transform.position = capsule2.transform.position;
             capsule2.transform.position = tempPosition;
        }
        yield return new WaitForSeconds(2.0f, 5.0f);
     }
 }
avatar image RandomUser123 · Aug 20, 2014 at 10:37 PM 0
Share

Thanks @$$anonymous$$Fitz91, I only realised that when you said it. @robertbu, this works much like the way I want it too, thanks for the help guys :)

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

23 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

Related Questions

Calling a function only once in Update 6 Answers

For loop resetting itself, but needs to stop 2 Answers

How do you change the current mesh of an object to the mesh of another object in your assets? 0 Answers

Is an object with an update function possible? (C#) 1 Answer

Update mesh collider after changing vertices at run time 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