#pragma strict
var ScoreUI : UI.Text;
var ScoreInt : int = 0;
var Score2Int : int = 0;
var Ball : GameObject;
var BallTransform : Transform;
var BallSpawn : Transform;
function Start () {
BallTransform = GameObject.FindGameObjectsWithTag("Ball");
}
function Update () {
ScoreUI.text = ("Home "+ ScoreInt+" - "+Score2Int+" Away");
}
function OnTriggerEnter (other : Collider) {
if (other.tag == "Ball")
{
ScoreInt += 1;
BallTransform.transform.position = BallSpawn.position;
ScoreUI.text = ("Home "+ ScoreInt+" - "+Score2Int+" Away");
}
}
I get the error “Cannot convert 'unityengine.GameObject[ ]'to 'unityengine.Transform”,
can someone give me the exact code that works?
BallTransform = GameObject.FindGameObjectsWithTag("Ball");
FindGameObjectsWithTag returns a GameObject, however your variable is of type Transform. Also note that your are using FindGameObjectsWithTag which returns multiple instances in an array if found. So your code should look like this:
BallTransform = GameObject.FindGameObjectsWithTag("Ball")[0].transform;
Note that this is still not error prone as you should probably check if any objects were found at all.
BBG_dev:
#pragma strict
var ScoreUI : UI.Text;
var ScoreInt : int = 0;
var Score2Int : int = 0;
var Ball : GameObject;
var BallTransform : Transform;
var BallSpawn : Transform;
function Start () {
BallTransform = GameObject.FindGameObjectsWithTag("Ball");
}
function Update () {
ScoreUI.text = ("Home "+ ScoreInt+" - "+Score2Int+" Away");
}
function OnTriggerEnter (other : Collider) {
if (other.tag == "Ball")
{
ScoreInt += 1;
BallTransform.transform.position = BallSpawn.position;
ScoreUI.text = ("Home "+ ScoreInt+" - "+Score2Int+" Away");
}
}
I get the error “Cannot convert 'unityengine.GameObject[ ]'to 'unityengine.Transform”,
can someone give me the exact code that works?
function Start () {
BallTransform = GameObject.FindGameObjectsWithTag("Ball").transform;
}