- Home /
OnClick to trigger function inside Update only once & using Vector3.MoveTowards inside that function call
Hi, I am not exactly sure the best way to proceed with my code.
I have something like this (C#) - JavaScript solutions also accepted:
public List<GameObject> prefabs = new List<GameObject>(); //Where 5 prefabs are added to the list via the Inspector
private List<GameObject> objects = new List<GameObject>();
public int num1 = 10, num2 = 5;
void Awake() {
for(int i = 0; i < num1; i++) {
for(int j = 0; j < num2; j++) {
objects.Add((GameObject) Instantiate(prefabs[j], Vector3(i, j, 0), Quaternion.identity));
}
}
}
public void Update() {
myFunction();
}
public void myFunction() {
int num = Random.Range(1, num1*num2);
for(int i = 0; i < num; i++) {
Vector3 currentX = objects[i].transform.position.x;
objects[i].transform.position = Vector3.MoveTowards(objects[i].transform.position, Vector3(currentX +1, objects[i].transform.position.y, objects[i].transform.position.z), Time.deltaTime);
}
}
This code creates 10 (num1) instances of 5 (num2) prefabs and places each object in a list to be easily accessed later. The
myFunction()
function then gets a random number based on num1 & num2 (50 in this case) and moves that many prefabs in the x direction by 1.
I am having issues linking the above code to an OnClick()
function. The OnClick()
function (from a UI button) should call the myFunction()
function from this script (attached to a separate object) when it is clicked. From there, the objects should move in the specified direction until they've reached their destination.
If I try the current code, all the prefabs jitter in space. Attaching it to the Onclick function makes the objects move 1 step and then the function ends.
How am I to incorporate Vector3.MoveToward() inside the for loop with the Update function (to make it move) as well as allowing the function to be triggered by an OnClick event?
Solutions using something else in the place of Vector3.MoveToward are accepted as well.
To sum it up: I am looking to move my objects from within the for loop (or somehow gaining access to specific items outside the for loop) in the Update function (using Time.deltaTime) triggered by the OnClick event. The function should only run ONCE
Any help regarding this is greatly appreciated! Thanks in advance
Not necessarily a solution, but a suggestion: Since myFunction() has to refer to "objects[i]" so often, maybe consider adding a small script to your prefabs allowing them to move themselves, then where you currently have myFunction just call:
objects[i].GetComponent("moveScript").start$$anonymous$$oving()
Also consider using a Coroutine if you want the prefabs to move over time until they reach a specific destination.
Answer by rageingnonsense · Mar 17, 2015 at 06:32 PM
This is something you would want to use a coroutine for. something like this:
public IEnumerator myFunction() {
int num = Random.Range(1, num1*num2);
for(int i = 0; i < num; i++) {
Vector3 currentX = objects[i].transform.position.x;
objects[i].transform.position = Vector3.MoveTowards(objects[i].transform.position, Vector3(currentX +1, objects[i].transform.position.y, objects[i].transform.position.z), Time.deltaTime);
yield return null;
}
}
void Update() {
if(Input.GetMouseDown(0)) {
StartCoroutine(myFunction);
}
}
Odds are this code wont compile EXACTLY as is (I just whipped it up), but this is the correct direction to go in. The coroutine will be executed each update, and continue where it left off. Once the loop is done, the coroutine will return and it will be done.
Read up on coroutines. they are very useful for stuff like this.
Your answer
Follow this Question
Related Questions
Smoothly moving a 2D gameobject along a vector path 1 Answer
How can I use unity 4.6 gui to have a health bar that follows my enemy in world space? 3 Answers
Override sorting not working? The canvas just freezes………zzzzz 0 Answers
xcode linker error 0 Answers
Update() in non-component script? 3 Answers