Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges