Hi, I created a script with a function spawning random objects and I’m trying to make the function static. The problem is I’m getting error for “transform”:
An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.
It’s the same error for lines 29, 33, 36 and 39. I’m new to scripting and to Unity, so I’ve never used static functions and I don’t know what to do. Here’s the whole script:
static var sObstacle : GameObject;
static var mObstacle : GameObject;
static var bObstacle : GameObject;
private static var spawnCount : float;
static var maxSpawnCount : float;
private static var randomPos : Vector2;
private static var obstacles : String[] = ["sObstacle", "mObstacle", "bObstacle"];
private static var obstaclesCount : int = obstacles.length;
private static var randomObstacle : int;
private static var obstacle : String;
static var ready : boolean;
function Update() {
}
static function Spawn() {
if(spawnCount < maxSpawnCount) {
ready = false;
spawnCount += 1;
randomPos = new Vector2(Random.Range(-1F, 1F), Random.Range(-1F, 1F));
randomPos = transform.TransformPoint(randomPos * .5F);
randomObstacle = Random.Range(0, obstaclesCount);
obstacle = obstacles[randomObstacle];
if(obstacle == "sObstacle") {
Instantiate(sObstacle, randomPos, transform.rotation);
}
else if(obstacle == "mObstacle") {
Instantiate(mObstacle, randomPos, transform.rotation);
}
else if(obstacle == "bObstacle") {
Instantiate(bObstacle, randomPos, transform.rotation);
}
}
else {
ready = true;
}
}