- Home /
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
if i understand i would have a script like above with a repeating function and then call move in each gameobjects script. example untested
public float launcheTime = 5f;
public float delay = 3f;
void Start()
{
InvokeRepeating( "$$anonymous$$oveAllObjects", delay, launchTime );
}
void $$anonymous$$oveAllObjects()
{
//Call the move function on gameobject 1
//Call the move function on gameobject 2
//Call the move function on gameobject 3
//And so one
}
example for calling scripts on another gameobject is found here
http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html
Answer by reineinmyheart · Nov 05, 2014 at 01:10 PM
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. :D
Answer by bubzy · Nov 05, 2014 at 01:21 PM
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
Your answer
Follow this Question
Related Questions
Simple move script 0 Answers
How Can I write a Click To Move Script? 2 Answers
move object! 0 Answers
how to make an object move around 3 Answers
Touch and move in any axis 0 Answers