- Home /
Cant set default var value to script?
I have a transform var on my script. And I want to drag and drop something from the hierarchy to the var slot in the inspector, but I cant. It highlights as if it will drop, but it remains to say "none" also, I was able to do it once! I dropped a none-prefab object from the hierarchy to the Inspector, and now I cant do it. I've been trying on this for days and you can understand my frustration.
also, I can drop prefabs into the var slot, but only prefabs. Here's my script if that helps.
var LookAtThis : Transform; var elaser : Transform; var damp = 2;
function Update () { if(Vector3.Distance(LookAtThis.position,transform.position) < 800){ var rotate = Quaternion.LookRotation(LookAtThis.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate,damp Time.deltaTime); transform.Translate(0,0, 10 Time.deltaTime);
}
} InvokeRepeating("fireAtPlayer",1,2);
function fireAtPlayer() { if(Vector3.Distance(LookAtThis.position,transform.position) < 800){ Instantiate(elaser,transform.Find("espawnpoint").position,transform.rotation); } }
function OnCollisionEnter(hit : Collision) { if(hit.gameObject.tag == ("Player")) Debug.Log("Watch it!"); }
Answer by Loius · Jan 03, 2011 at 08:08 PM
You can't drag game objects to scripts' default values. If you have an actual object with this script attached, you'll be able to drag game objects to its properties, otherwise you can only use prefabs.
Okay, I understand. I must be going about this all wrong then, the way I'm trying to do it is, create an enemy prefab that spawns and goes towards the player,which I've tried as a prefab and a simple game object, shooting at him and so on. I am using transform var to define the player as a target. Is this the correct method for enemy movement? because I have no idea how to do it otherwise,aside from using gameOnject.name which doesn't seam to work too well with transform.position. If you think it best I'll post this as a new question.
If you have a Player object and an Enemy object in your hierarchy, you can drag the Player object to the Enemy's script and it'll work. THe problem with just using prefabs is the enemy will look at the player prefab's position, which doesn't change. If you don't want to do it like that (you don't) you can tag the player object "player" and find it in the enmy by saying "playerTransform = GameObject.FindObjectWithTag("player")"
*the problem with using prefabs ins$$anonymous$$d of gameobjects as default values