• 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 ctwheels · Mar 17, 2015 at 03:14 PM · updateonclickmovetowardsunity4.6c#-and-js

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

Comment
Add comment · Show 1
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 emayberry · Mar 17, 2015 at 03:50 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Smoothly moving a 2D gameobject along a vector path 1 Answer

Unsure why I am getting errors 1 Answer

MoveTowards position isn't effected by rigidbody's velocity. Why? 1 Answer

How to get a proper ramming effect? 0 Answers

Move multiple gameobjects 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges