I have this code
var Player : Transform;
var Dead : boolean = false;
function Update () {
var dist : float = Vector3.Distance(Player.position, transform.position);
Debug.Log(dist);
if(dist <= 30){
Dead = true;
}
if(dist >= 200 Dead==true)
{
Destroy (gameObject);
}
}
But the crap about this is that i have to add, the gameobject to each object there need this script
I have tryed to used gameObject.find(“Cplayer”); //Cplayer is the name of the players object.
But i got errors about reference, and crashing unity, why?
How to solved this code?
Player = GameObject.Find("Cplayer").transform;
You’ll want to put this in the Start function. Note that “gameObject” references the current GameObject that your script is attached to. “GameObject” (with an upper case G) references the Class and lets you call the method “Find”.
Im getting an error
Assets/Scripts/DestoryDistance.js(8,37): BCE0022: Cannot convert ‘UnityEngine.Transform’ to ‘UnityEngine.GameObject’.
var Player : GameObject;
var Dead : boolean = false;
function Start (){
Player = GameObject.Find("Cplayer").transform;
}
function Update () {
var dist : float = Vector3.Distance(Player.position, transform.position);
Debug.Log(dist);
if(dist <= 30){
Dead = true;
}
if(dist >= 200 Dead==true)
{
Destroy (gameObject);
}
}
you init the player as game object ,and casting as a transform .
var player:Transform ;
player=GameObject.Find("name of it ").transform;