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.

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.

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

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.