i have a simple script that respawns the enemy when it dies. but when the enemy respawns, the script pauses cause there isnt anything set to the target variable. no matter how many times i add a targeting function, no matter where i place it, it spawns and just sits there without doing anything.
target = gameObject.FindWithTag("player");
var prefabExplosion:Transform;
var target:Transform;
function Update () {
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
transform.position += transform.forward * 30 * Time.deltaTime;
}
function LateUpdate(){
target = gameObject.FindWithTag("player");
}
function OnTriggerEnter(other:Collider){
Destroy(gameObject);
Instantiate(prefabExplosion, transform.position, Quaternion.identity);
}
i give up on it cause its just irritating me too much.
if anyone has some information on what im doing wrong, i really need it.
Is this the whole script and does it not cause errors when compiling?
Because the first line target is a gameobject and you use unity’s default target variable.
Then there is a variable target that is a transform on line 3.
Assigning a target in lateupdate looks weird to me too as you assign a target (of a gameobject type) after the update that does the target movement.
Since the target type you assign the player too is a gameobject, the players position would be:
var relativePos = target.tranform.position - transform.position;
I would find it weird if this script compiles and works until a respawn.
If you instantiate the enemy on spawn or have only 1 player, you can assign the target in the Awake() or Start() function. (asuming the player exists at that moment)
Else you may have to assign a target using a trigger script that activates the enemy when in range.
you didnt help at all. i said despite all i tried, that means i started fucking with the code, it wouldnt work, so i just left it like that. theres a different respawn script… i didn’t want a code fix of that sort.
i have stried start, awake, putting it on every line in every section, nothing seems to work. when it spawns, it says nulltransformreference and the script freezes up. i have to assign the target manually for it to unfreeze.
put target = gameObject.FindWithTag("player"); in Start or Awake (instead of the places you have it).
If you're getting an error, double-click the error and it'll pop up the script exactly on the line that's causing the problem. Given that, we can more quickly solve the problem.
Change "gameObject" to "GameObject". They are two different things.
ok you are underestimating my scripting skills, i tried doing stuff i logically wouldn’t do, i tried having it with .transform at the end, without, every variation of gameObject, putting it in start, im starting to think it might be a unity error, my project has been crashing alot so… thats probably it.