- Home /
instantiate. Code Problem. Help.
Hello guys im trying to create my first game 2D but i face some troubles.. I have made 3 prefabs of 1)small ground 2)medium ground 3)big ground i have set my player to move to the right with some speed and the purpose of the game is to jump at these ground and try to make the best time without to fall out of them... like a jetpack..
now i want my smallground to instantiate it and as the player moves i want more grounds to generate automatically in random range.. so far i wrote this code.. but the problem with this code is that it generates only 1 small ground at random range and not lot of them .. my code:
#pragma strict
var smallG: Transform;
function Start () {
var position: Vector3 = Vector3(Random.Range(-10.0,10.0),0,Random.Range(-10.0, ``10.0); Instantiate(smallG, position, Quaternion.identity); }
function Update () {
}
what i have to add so to create gorunds all the time? and second question i made the idea for this code from the scripting reference.. but i dont know why i put the code in the start method and not to the update method?
Answer by Dblfstr · Feb 25, 2014 at 03:41 PM
Start is called once, when the script is first activated. So you are only going to Instantiate one object. You need to have a spawn function that you call in Start, then have the spawn funciton call itself after a specified amount of time.
pragma strict
var smallG: Transform;
function Start () {
//Call the spawn function when the level is loaded.
Spawn();
}
function Update ()
{
}
//The spawn function will instantiate your object
function Spawn()
{
var position: Vector3 = Vector3(Random.Range(-10.0,10.0),0,Random.Range(-10.0,10.0);
Instantiate(smallG,position, Quaternion.identity);
Invoke("Spawn",5);
}
i checked lot times... but i get 2 errors.
1)Assets/Scripts/Instantiate!.js(25,20): BCE0070: Definition of 'Instantiate!.Spawn()' depends on 'Instantiate!.Spawn()' whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.
2)Assets/Scripts/Instantiate!.js(25,19): BCE0023: No appropriate version of 'UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine' for the argument list '(function(): unknown)' was found.
That's because you have an infinite loop... You can't keep starting the same coroutine from inside that same coroutine
And the point of this is to create an infinite loop. One that will continue to spawn objects. You will need to stop this when your player dies, or when you reach the end of a level... if there is an end. This is for an infinite scenario, like an infinite runner.
thats the name of my game.. but i have a problem again!
i enter this code:
var position: Vector3 = Vector3(Random.Range(1.0,10.0),0,Random.Range(200.0,100.0));
Instantiate(smallG,position, Quaternion.identity);
//wait 2 seconds
yield WaitForSeconds(2);
//Call the spawn function again
StartCoroutine("Spawn",2);
i entered this script in other 2 prefabs.. at the first 5 metres all ok.. but after the 5 metres nothing appears no more grounds create... i have put range at x cordinates from 1.0 to 100.0.. so whats wrong? it should continue to create brackets "forever" untill 100.0 .. correct?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
XBox Controller Angle of Fire Incorrect 0 Answers
How to Destroy the instances of 2 gameObjects at the same time? 1 Answer
instantiate gameobject with different size? 1 Answer
Player instantiates Backwords 0 Answers