- Home /
Cannot conver UnityEngine.Transform to UnityEngine.GameObject
I have this annoying error with my script, how do I fix it? My script: var TactScript : ThrowTact;
public var Health : float = 100.0f;
var Player : GameObject;
var DefSpawn : Vector3;
var Tact : GameObject;
function Update () {
Tact = GameObject.FindGameObjectWithTag("Tact").transform;
if(Health == 0 && TactScript.HasTact == true){
Player.transform.position = Tact.transform.position.up * 3;
Health = 100;
}
else
if(Health == 0 && TactScript.HasTact == false){
Player.transform.position = DefSpawn.transform.position;
Health = 100;
}
}
Answer by brain56 · Nov 15, 2013 at 07:20 AM
The problem is this line:
Tact = GameObject.FindGameObjectWithTag("Tact").transform;
Tact
is a GameObject, and you're assigning the Transform
of the GameObject
tagged "Tact"
. A `Transform` cannot be converted into a `GameObject`, even if typecasted.
What you should do is delete the .transform
at the end of that line. That should fix the error.
Ok, but if I want to go up in Y by 3 if it's a transform, how do I do that?
Answer by zanearn · Nov 15, 2013 at 07:18 AM
Change this line:
Tact = GameObject.FindGameObjectWithTag("Tact").transform;
To:
Tact = GameObject.FindGameObjectWithTag("Tact");
Answer by Mefistofel · Nov 15, 2013 at 07:21 AM
Transform - is the component of Game object(it added by default). Your error - DefSpawn is Vector3, it does not contain transform, this is simple 3d vector. Player.transform.position = DefSpawn;