Instantiate at random point - static fn problem

I want to spawn an object at a random location. I have the following function SpawnTile() attached to an empty game object. I call the SpawnTile() function from another script with this:

function OnSwipe( e : FingerGestures.Events.Swipe )

static function SpawnTile () {
	//Spawn at random location
	var position: Vector3 = Vector3(Random.Range(0, 400.0), 420, 200);
	transform.position = position;		
	Instantiate(newTile, position, transform.rotation);

I get an error:
Assets/Scripts/Create.js(16,9): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.

Remove the “static” in “static function SpawnTile ()”. Change “Create.SpawnTile()” to “SpawnTile()”. This way you don’t call the function using a static context.

SpawnTile() is the same that self.SpawnTile() and isn’t a static call. Create.SpawnTile() is a static call and you can even use it without having this script as component. Basically, you only can call a non-static method after you have some object instance (using “new”). This is a basic object oriented concept,

I recommend you to search about Object Oriented and Static vs Non-Static.

You can’t call object variables (like transform) in a static method, although you can set some static variable to be the reference of object and call the transform of this variable.