- Home /
I found the Problem and am now trying to solve it, by assigning the var Spawn(1,5) to the gameobject in the hierarchy instead of the asset folder
Doesn't Instantiate at the assigned EmptyObject
I HAVE FOUND THE PROBLEM, I SHOULD NEVER HAD MADE THOSE PREFABS BECAUSE THEY DON"T UPDATE ON RUN, SO I ASSIGNED THE SPAWNS TO THE GAMEOBJECT IN THE HIERARCHY INSTEAD
Hello fellow ForumMembers,
I found a way to Instantiate my Obstacles to an empty gameobject. The next problem I encounter is that when I try to move the empty gameobject ,where it suppose to be spawned. It doesn't update its position. Also it seems like that the objects keep spawning at that same position even when I move the gameobjects to another position before pressing on start.
This is the script I made
var Spawn1 : GameObject;
var Spawn2 : GameObject;
var Spawn3 : GameObject;
var Spawn4 : GameObject;
var Spawn5 : GameObject;
//The Obstacle
var Obstacles : GameObject;
InvokeRepeating("Start2", 0, 1.0);
function Start2()
{
if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){
var Spawn = Random.Range(0,4);
if(Spawn == 0){
Debug.Log("spawn0");
Instantiate(Obstacles, Spawn1.transform.position, Quaternion.identity);
}
if(Spawn == 1){
Debug.Log("spawn1");
Instantiate(Obstacles, Spawn2.transform.position, Quaternion.identity);
}
if(Spawn == 2){
Debug.Log("spawn2");
Instantiate(Obstacles, Spawn3.transform.position, Quaternion.identity);
}
if(Spawn == 3){
Debug.Log("spawn3");
Instantiate(Obstacles, Spawn4.transform.position, Quaternion.identity);
}
if(Spawn == 4){
Debug.Log("spawn4");
Instantiate(Obstacles, Spawn5.transform.position, Quaternion.identity);
}
}
}
InvokeRepeating let me call the function each second.
var Spawn 1: GameObject; is an empty Object were the Obstacles should be Instantiate.
Random.Range let me to randomize the position of the Obstacle each time the function has been called.
here have a picture.
thx for reading.
I experiment with my project and it seems the problems are the prefabs, the position of prefabs doens't change in the Asset folder, but those are actually the standart position for the instaniate.
Answer by JacobHockey13 · May 05, 2014 at 04:56 PM
var Spawn1 = Vector3(0,0,0);
var Spawn2 = Vector3(0, -1, 0;
var Spawn3 = Vector3(0, -2, 0) ;
var Spawn4 = Vector3(0, -2, 0);
var Spawn5 = Vector3(0,-2, 0);
//The Obstacle
var Obstacles : GameObject;
InvokeRepeating("Start2", 0, 1.0);
function Start2()
{
if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){
var Spawn : int = Random.Range(0,4);
if(Spawn == 0){
Debug.Log("spawn0");
Instantiate(Obstacles, Spawn1, Quaternion.identity);
}
if(Spawn == 1){
Debug.Log("spawn1");
Instantiate(Obstacles, Spawn2, Quaternion.identity);
}
if(Spawn == 2){
Debug.Log("spawn2");
Instantiate(Obstacles, Spawn3, Quaternion.identity);
}
if(Spawn == 3){
Debug.Log("spawn3");
Instantiate(Obstacles, Spawn4, Quaternion.identity);
}
if(Spawn == 4){
Debug.Log("spawn4");
Instantiate(Obstacles, Spawn5, Quaternion.identity);
}
}
}
Just change the variables to the desired positions. I would also recommend you declare variables for your new instantiated objects.
Well, your script is some kind the same as $$anonymous$$e. The only difference is that I can set the spawn to an empty object I set in the scene. With this I can easily change the spawn positions while your script is bound to an exact position. Spawning them in the wished Y.X.Z position wasnt really the problem.
I had those spawn position added as a Child of the camera. So when the camera moves ,the spawnpoints will also move along. The problem was that it didnt update the location of the spawnpoint. Even when I manually re position the spawnpoints.
So ins$$anonymous$$d of having a var :gameobject;
I made a var spawn1 = GameObject.Find("spawn1"); and put it in the function start2() This worked out well.
Thanks anyway for your response. ~Hexer
Follow this Question
Related Questions
Calculating where to position instantiated clone 1 Answer
Classes VS Variable Scripts 1 Answer
Shotgun Spread Issue 1 Answer