- Home /
GameObject.FindGameObjectWithTag is not working
Hi, this is a continuation of the topic "How to teleport a object", so I am trying to assign a object at runtime, but it doesn't work at all and this is so frustrating and I just want it to work, how do I assign it at runtime correctly? My issue is that it doesn't get assigned and I spawn at the center of the scene at 0, 0, 0.
Here's my script:
var TactScript : ThrowTact;
static var Health : float = 100.0f;
var DefSpawn : Vector3;
var Tact = GameObject;
function Start () {
}
function Update () {
Tact = GameObject.FindGameObjectWithTag("Tact");
if(Health == 0 && TactScript.HasTact == true){
transform.localPosition = DefSpawn;
Health = 100;
}
else
if(Health == 0 && TactScript.HasTact == false){
transform.localPosition = Tact.transform.localPosition.up * 3;
Health = 100;
}
}
Oh yeah, but I just copied my script to give you a reference but it has a colon now.
Answer by clunk47 · Nov 13, 2013 at 09:06 PM
It's because you don't use '=' to define a type. You use a ':'. Change Tact = GameObject to
var Tact : GameObject;
You also shouldn't be defining in Update(). Move
Tact = GameObject.FindGameObjectWithTag("Tact");
to Start()
Answer by Griffo · Nov 13, 2013 at 08:17 PM
Try this -
var TactScript : ThrowTact;
static var Health : float = 100.0f;
var DefSpawn : Vector3;
private var tact : GameObject;
private var loop : boolean;
function Start () {
}
function Update () {
if(!loop){
loop = true;
findGameobject();
}
if(Health == 0 && TactScript.HasTact == true){
transform.localPosition = DefSpawn;
Health = 100;
}
else
if(Health == 0 && TactScript.HasTact == false){
transform.localPosition = Tact.transform.localPosition.up * 3;
Health = 100;
}
}
findGameobjet(){
tact = GameObject.FindGameObjectWithTag("Tact");
}
Well, i've managed to assign it at runtime now, but there's another problem as well. And that problem is that when I die an everything and the object is assigned I spawn at the world center as if my object's transform isn't assigned or something.