• 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
Question by Velketor · Dec 30, 2012 at 11:31 PM · javascriptgameobjectfunctionrestart

Restart this script

How can I restart this script when it's finished running? I just want it to repeat the entire script over and over infinitely. Here is the script:

 var target : Transform; //the enemy's target
 var moveSpeed = 10; //move speed
 var rotationSpeed = 310; //speed of turning
 
 var myTransform : Transform; //current transform data of this enemy
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
 
 function Start()
 {
      target = GameObject.FindWithTag("aggro").transform; //target the player
 
 }
 
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

Any help is greatly appreciated. Thank you.

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by GlennHeckman_old · Dec 31, 2012 at 06:05 PM

Shawn, Here are some tips for a more efficient script, and hopefully and answer to your question:

  • There is no need to save the enemy's transform data into a variable, you can just access it directly by typing:

    transform.position

  • It is silly to call a repeat of a function every 1/Billionth of a Second, 1/10 or so will work just fine (.1).

  • Look into Coroutines as well if you think you'll need to do a lot of looping stuff: Coroutine

  • There is no need to InvokeRepeating an Awake or Start function. They are meant to only run one time.

  • InvokeRepeating an Update function is a waste of processor power and is pointless because the Update function will repeat itself anyways. It's what it does; similar to OnEnterFrame for ActionScript.

  • If you want to repeat a function, create a new one and call that. Here's an example: > function Start() > { > InvokeRepeating("TestFunction",5); > } >
    > function TestFunction() > { > print("Test Function every 5 seconds"); > }

  • You should type cast your variables so it makes it easier for debugging, and overall readability of your code. > var target:GameObject; > var moveSpeed:int = 10; > var rotationSpeed:int = 5000;

  • Your Start function is where you should declare your variables: > function Start() > { > // Enemy's Target > target = GameObject.Find("aggro"); > }

  • The Update function is where you want code to execute every frame (like ActionScript EnterFrame).
    > function Update () > { > //rotate to look at the player > transform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.transform.position - transform.position), rotationSpeed*Time.deltaTime); >
    > //move towards the player > transform.position += transform.forward moveSpeed Time.deltaTime; > }

Hope this helps!

  • Glenn

Comment
Velketor

People who like this

1 Show 1 · 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
avatar image Velketor · Dec 31, 2012 at 07:28 PM 1
Share

thanks Glenn. you are right. i moved everything into the Update function and it magically works now. accepting your response as correct.

avatar image

Answer by ZorNiFieD · Dec 31, 2012 at 01:06 AM

The script does continue running over and over. Anything in the Update() routine is running repeatedly. What is not repeating for you?

If you want to target the player again, just move the line:

 target = GameObject.FindWithTag("aggro").transform; //target the player

from the Start function to the Update function.

Comment
Bunny83

People who like this

1 Show 3 · 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
avatar image Velketor · Dec 31, 2012 at 07:31 PM 0
Share

hey I want to accept GlennHeckman's answer instead of this answer. it won't let me remove the accepted status from this question and give it to Glenn's question. any thoughts?

avatar image whydoidoit · Dec 31, 2012 at 07:50 PM 0
Share

Done it for you

avatar image Velketor · Dec 31, 2012 at 07:55 PM 0
Share

thank you so much!

avatar image

Answer by Velketor · Dec 31, 2012 at 02:34 PM

Thanks for the help. I found it's really a different issue for me. Instead of searching for all the gameObjects by tag, I found it would be better to reference the owner of the object instead (since that is who I am looking for). Here is the updated script:

 var target = GameObject.Find("Aggro").transform; //the enemy's target
 var moveSpeed = 10; //move speed
 var rotationSpeed = 5000; //speed of turning
 var myTransform = this.transform; //current transform data of this enemy
 
 InvokeRepeating("Start", .1, 1);
 InvokeRepeating("Update", .1, .0000000001);
 InvokeRepeating("Awake", .1, .0000000001);
 
 
 
 function Awake()
 {
     myTransform = this.transform;
 }
 
 function Start()
 {
      target = GameObject.FindWithTag("aggro").transform; //target the player
 
 }
 
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
 

However, it is still only working for the FIRST prefab that is spawned. After that, the script doesn't execute the rotation for new gameObjects. Basically, this script is only working on 1 object, the first spawned. I'll accept your answer since it was correct. I am now switching up my question a bit and I hope you can help answer that too =) thanks good sir.

Comment

People who like this

0 Show 4 · 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
avatar image Bunny83 · Dec 31, 2012 at 04:13 PM 0
Share

Sorry, but that makes no sense what you did there. Awake, Start and Update are special callbacks that are called automatically when it's time to. You really shouldn't call those functions manually or by using InvokeRepeating on them.

Update is called every frame so calling it again doesn't make any sense.

It seems you want to search for a new target every second, in this case create your own function for that but don't use Start.

Use meaningful names, unity did the same. Start is called once at Start of the game / lifetime of the script. Update is called when the script needs to be updated which is once per frame.

I guess you want something like that:

var target : Transform; //the enemy's target var moveSpeed = 10.0; //move speed var rotationSpeed = 5000.0; //speed of turning private var myTransform = this.transform; //current transform data of this enemy

 function Start()
 {
     myTransform = this.transform;
     InvokeRepeating("FindNewTarget", 0.001, 1.0);
 }
 
 function FindNewTarget()
 {
     target = GameObject.FindWithTag("aggro").transform;
 }
 
 function Update ()
 {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
avatar image Velketor · Dec 31, 2012 at 04:28 PM 0
Share

thanks for the help. although I received no errors, the updated script you provided still functions exactly the same as my old script. the 2nd prefab has no problem moving toward my player. it has a problem rotating toward the player. it will rotate toward the player for a split second and then it looks back it's normal direction and then it repeats that over and over.

avatar image Bunny83 · Dec 31, 2012 at 04:49 PM 0
Share

That doesn't make sense. If my script does something else than yours there has to be another script that cause this. Are you sure you don't have two scripts which might control the same object?

What you did is just wrong. It's like you say "i drive my car backwards". Of course it works but it's incredible inefficient and may have unwanted sideeffects.

avatar image Velketor · Dec 31, 2012 at 05:53 PM 0
Share

It is only 1 script. If I am driving my car backwards then I apologize. I don't know how to do it properly yet and I am still learning. Thanks for trying to help me.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Quest Script Help 2 Answers

how to get the name of an object that a script is attached to 1 Answer

Referencing children of an instantiated object. 1 Answer

Created game objects rollover show guy text 0 Answers

{Easy question} How to disable script from other gameObject. 2 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