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;
  }

}

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()

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");
}