Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CanadianOtaku · Nov 23, 2014 at 08:18 PM · javascriptarray

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;
 }
 }






Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CanadianOtaku · Nov 23, 2014 at 08:08 PM 0
Share

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);
  }
  }
avatar image YoungDeveloper · Nov 23, 2014 at 08:47 PM 0
Share

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 ().

avatar image robertbu · Nov 23, 2014 at 08:57 PM 0
Share

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().

avatar image CanadianOtaku · Nov 23, 2014 at 09:21 PM 0
Share

@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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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...
     
     }
 

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CanadianOtaku · Nov 23, 2014 at 10:14 PM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Items with Statistics(such as attack damage) that actually effect the character? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Sprint Error script? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges