- Home /
"transform" in a static function
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;
}
}
In the Instantiate, transform.rotation
says the new object should copy the rotation of whatever gameObject the script is on. A lot of spawn scripts that use transform.position/rotation
are meant to go on empties. That way you can move/spin the empties to set the spawn points.
Static means (in Unity) that you never count as being on any gameObject. So, in order to even start making it a static, you have to figure out what rotation you want to new thing to have. To just get it to work at all, you could put Quaternion.identity
.
Okay, thanks to your and $$anonymous$$elly$$anonymous$$'s answers I understand it way better. Thank you.
Answer by KellyThomas · Apr 27, 2014 at 02:06 PM
transform
is an instance member of your script (inherited from Component).
If you want to access a transform from another context (like a static method) then you are responsible for making one available. One way to achieve this is to pass it as a parameter.
Consider this example:
static function Spawn(otherTransform: Transform) {
if(spawnCount < maxSpawnCount) {
ready = false;
spawnCount += 1;
randomPos = new Vector2(Random.Range(-1F, 1F), Random.Range(-1F, 1F));
randomPos = otherTransform.TransformPoint(randomPos * .5F);
randomObstacle = Random.Range(0, obstaclesCount);
obstacle = obstacles[randomObstacle];
if(obstacle == "sObstacle") {
Instantiate(sObstacle, randomPos, otherTransform.rotation);
}
else if(obstacle == "mObstacle") {
Instantiate(mObstacle, randomPos, otherTransform.rotation);
}
else if(obstacle == "bObstacle") {
Instantiate(bObstacle, randomPos, otherTransform.rotation);
}
}
else {
ready = true;
}
}
//...
function Update() {
Spawn(transform); // pass the Transform of the current GameObject
}
Your answer
Follow this Question
Related Questions
Better way to call function from another script from editor script? 1 Answer
Access a function from one script in another? 3 Answers
rot not working for 2nd prefab 2 Answers
Setting Position of Spawned Prefab 2 Answers
function Start () problem!! 3 Answers