- Home /
I want to find a transform when an object is instantiated
hey, I`m trying to make a turret spawn when there are no more turrets left is the scene, but when it spawns the Player`s transform variable is empty and i cant find the way to make it find so here is what i was trying:
var LookAtTarget:Transform;
function Start () {
LookAtTarget = GameObject.Find("Player");
}
function Update ()
{
distance = (LookAtTarget.position - myTransform.position).magnitude;
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * damp);
var seconds : int = Time.time;
var evenodd = (seconds / 2);
if (evenodd && distance < maxDistance)
{
Shoot(seconds);
}
}
(i just copied what i thought was necessary because the code is too long) If some one has an idea of how to make him automatically find its target i would much appreciate it.
thanks to all
Answer by Epixam · Oct 27, 2012 at 05:18 PM
Perhaps part of your problem is when you do
var LookAtTarget:Transform;
function Start () {
LookAtTarget = GameObject.Find("Player");
}
you are trying to set the "transform" variable to a "gameobject. That cant be helping things.
I think you need to do this
LookAtTarget = GameObject.Find("Player").transform;
Also REMEMBER that since you are instantiating the player you will need something in the update to see if the variable is NULL and if it is then find the player
function Update()
{
If(LookAtTarget == null)
LookAtTarget = GameObject.Find("Player");
else
{
// do update stuff
}
}
Answer by Montraydavis · Oct 27, 2012 at 03:39 AM
I tested this, and it works.
var prefabb : GameObject;
prefabb = Instantiate (gameObject, Vector3.zero, Quaternion.identity);
var blah : Transform = prefabb.gameObject.transform ;
PS: Just replace 'gameObject' with the instantiated object.
thanks a lot, still it does not work for me, or i don`t know how to use it. so, is this supposed to make the already deployed turret to find the player?
Welcome. :) Feel free to hit thumbs up if it worked.