- Home /
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?
Get rid of the timer code and use InvokeRepeating() to call this function once every 5 seconds.
whenever hasChanged is set to true, it is immediately set to false again... you may want to look into that
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;
}
}
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 :)
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);
}
}
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
![](https://koobas.hobune.stream/wayback/20220613161025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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