Move many objects repeatdly

Hi,

In my scene I have 2 GameObjects, a seesaw that goes up and down and a gear that rotates, everyone with its own script with the move. I like to have a unique script that controls the time and move both launching it repetedly.

My problem is how to call GameObects and its own move scripts.

I have this now:

#region variables
public float launchTime = 5f; // Time between moves.
public float delay = 3f; // Delay time.
#endregion

void Start () {
    // Start calling the Move function repeatedly after a delay.
    InvokeRepeating("Move", delay, launchTime);
}

// Launch move scripts
void Move()
{        
    // I dont know how to implement ¿Maybe I catch objects with a Tag?
}

Thank you

You would be doing something in this manner…

public GameObject other_object;

void Start()
{
  other_object = GameObject.FindGameObjectWithTag("TagOfObject");
}

void Move()
{

other_object.GetComponent<NameOfScript>().function_name(); 
//make sure the function you're calling is public

}

but that only works if you’re looking for one object.
if however, you’re going to call multiple objects with the same tag…you can do something like this…

public GameObject[] other_objects;

void Start()
{
   other_objects = GameObject.FindGameObjectsWithTag("TagOfObject");
}

void MoveAllObjects()
{
   foreach(GameObject other_object in other_objects)
   {
     other_object.GetComponent<ScriptName>().FunctionName();
   }
}

Basically, you’re storing each object in a list.
Hope that helps. :smiley:

Why not just have the movement code in each object? Then you can just have a public bool to reference to turn the movement on or off