- Home /
I need help with an array
I need some help putting an array together so when I press Q it will choose one of the rooms and then place it where I told it to go.
public var button : boolean;
private var enter : boolean;
var particle : ParticleSystem;
var RoomArray : String[] = ["room", "room2", "room3", "room4"];
var chosenRoom : String;
var room : GameObject;
var room2 : GameObject;
var room3 : GameObject;
var room4 : GameObject;
function Update (){
if(button)
{
}
if(Input.GetKeyDown("q") && enter){
button = !button;
animation["Push"].wrapMode = WrapMode.Once;
animation.Play("Push");
particle.Play();
room.transform.position = Vector3(0,0,20);
room2.transform.position = Vector3(0,0,30);
room3.transform.position = Vector3(0,0,40);
room4.transform.position = Vector3(0,0,50);
}
}
function ChooseRoom()
{
chosenRoom = RoomArray[Random.Range(0, RoomArray.length)];
Debug.Log("chosenRoom " + chosenRoom);
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'Q' to change door");
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
Oh sorry nothing is doing anything wrong the code is all fine. I guess I should have said can someone help me choose one of the rooms? You see what happens is when I press Q it will spawn in all the rooms I only want it to spawn in one at a time so it would be this part of my code that needs altering, I'm not quite sure I'm still pretty new to this.
function Update (){
if(button)
{
}
if(Input.Get$$anonymous$$eyDown("q") && enter){
button = !button;
animation["Push"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation.Play("Push");
particle.Play();
room.transform.position = Vector3(0,0,20);
room2.transform.position = Vector3(0,0,30);
room3.transform.position = Vector3(0,0,40);
room4.transform.position = Vector3(0,0,50);
}
}
It doesn't spawn anything, your simply changing the transform position. To randomly spawn in one of these rooms you can use Random.Range(). For actually spawning you should use Application.Loadlevel(). If your rooms arent actual scenes, them simply pick on random position from all rooms to place your player, using mentioned random.range ().
The question is fuzzy enough that I cannot give you a full answer, but you want to declare your rooms game objects in an array:
public var rooms : GameObject[];
Then in the inspector, you will open this array, set it size to 4, and the then drag and drop the room game object into the array. Once in an array, you can randomly generate a room selection just like you do for the name function ChooseRoom().
@robertbu that is what I'm looking for but I can't seem to get it to work with my function ChooseRoom() how would you go about setting it up the code?
Answer by psycocrusher · Nov 23, 2014 at 09:53 PM
function ChooseRoom(){
//first element in the array
chosenRoom = RoomArray[0];
//Second element in the array
chosenRoom = RoomArray[1];
//third element in the array
chosenRoom = RoomArray[2];
//etc...
}
I only have one problem now and that is that it will not show the name of the rooms beside the chosen room in the inspector, is that because everything else starts when Q is pressed?