- Home /
Instantiating a prefab and setting its variables
Hello all , I'm new in unity. Got stuck with a certain problem. I want to create an endless runner platform generation script.The basic logic that i want to implement is that
1.Each platform has a collider frame in the middle. 2.Upon triggering a collision the next platform will get generated having similar collider in the middle. 3.Thus the script aims to keep on generating a new platform as the colliders are hit in successsion.
#pragma strict
var prefabPlatform : GameObject = Platform ;
var testvalue: int =5 ;
function Start () {}
function Update () {}
function OnTriggerEnter (other : Collider)
{
var new_prefab : GameObject;
var childObject=other.gameObject;
print(other.gameObject.name+" with tag: "+other.gameObject.tag+" has hit the "+gameObject.name);
if(other.gameObject.tag.Equals("runner"))
{
new_prefab=Instantiate(prefabPlatform, Vector3(0, 0, -1*20), Quaternion.identity);
print("instantiating prefab: ");
}
else
print("Wrong Hit !!!");
}
Where i am stuck is For the second gameobject that is generated how do i set the prefabPlatform to dynamically get assigned to a random prefab ?
Basically i want to var prefabPlatform : GameObject =(Some Code to set a random prefab from a list of prefabs i have made )
Answer by KoshX87 · Jun 22, 2013 at 06:54 AM
ok for random generation of platforms you are gonna need to do 2 things. First you need to make every different type of platform you want to have at the moment. You don't need to do them all right now, but probably best if you have 3 for testing purposes. Lets say. one platform is blank, another platform has like a thing you have to jump over(i.e. a hole) and the third platform has something you have to slide under. The functionality of these is up to you, but i'm just saying what each platform does rather than saying platform 1,2 and 3 as it is less confusing.
You know that the empty platform with no enemies or hazards on it needs to be occurring more often than the other platforms. It will be included in the array we are about to make, but i think its important that we understand that this platform will need to happen quite frequently relative to the other platform types.
After you have all the platform types you need. write in the following code:
var platformArray: Array[];
var emptyPlatform: GameObject;
// dont worry about this for now var randNumber: int;
Ok. Save this code as a new javascript file and call it "platformGen.js". Make an empty object in your scene and call it "Generator". Drag "platformGen.js" to the "Generator". While you have the generator selected go to the inspector and in the platformArray drop down menu, change its size to the amount of (platforms you want - 1) and then fill each of the empty slots in the inspector with a platform.
Also in the inspector drag the empty platform you have to the emptyPlatform spot.
Now the code below should be placed inside the if(other.gameObject.tag.Equals("Runner"){}:
randNumber = Random.Range(0, platformArray.Length);
new_prefab=Instantiate(platformArray[randNumber], Vector3(0, 0, -1*20), Quaternion.identity);
Finally, all that is left to do is code the script such that the instaniate above happens, once and then the empty platform is defintely the next platform. If you don't, the player will be spamming buttons the whole time to stop themselves from dying (haha). To do this you can simply make a boolean variable and when its false, you make a empty platform. After an empty platform is made, the boolean variable can be set to true and so then it will activate the code I have mentioned above.