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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alp-Giray-Savrum · Jan 27, 2014 at 03:16 PM · randomspawningarearandomspawninglimitations

Create Objects at Random Place on a Plane

Hello guys, I've been working on "Random" things. So I couldn't get it exactly.

I've got 5 diffrent NPC spaceships. 2 types of asteroid, 3 types of bonus things and a Spawner Plane.

So, Spawner Plane's scale = (0.6, 0.6, 0.6).

I want to create thoose objects at inside of the plane. But i dont know how to assign them into array. Could anyone help me thanks.

Age 14, From Turkey.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Aladine · Jan 27, 2014 at 07:22 PM

Hi,

in order to create an array of game objects (better go with Transforms) you need something like this :

  • 1st create a list

       public List<Transform> myList = new List<Transform>();
    
    
  • 2nd add your objects to that list, for now i'll advice you to do it in the inspector by changing the size of your list to 3 and then just drag and drop your game objects into the list.

  • now is the random placing part and actually it's quite easy, all you need is to generate a random Vector3 (or Vector2 if it's 2d) based on your plane position and scale

  • Example

    //this generate random coordinates isinde the plan, assuming that you already get your plan and stored it in a variable called plane float randomX = Random.Range (plane.transform.position.x - plane.transform.localScale.x / 2, plane.transform.position.x + plane.transform.localScale.x / 2); float randomY = Random.Range (plane.transform.position.y - plane.transform.localScale.y / 2, plane.transform.position.y + plane.transform.localScale.y / 2); float randomZ = Random.Range (plane.transform.position.y - plane.transform.localScale.z / 2, plane.transform.position.y + plane.transform.localScale.z / 2); Vector3 randomPosition = new Vector3 (randomX, randomY, randomZ);

  • now back to your list, you need to assign that random position to your objects (keep in mind to redo the process for each objects otherwise they will all go to the same position)

    myList[0].position = randomPosition;

do the same thing for other elements but only after re-randoming the position Important in order to use Lists make sure to import Generic so you have to add this line in the top

 using System.Collections.Generic;

 
Comment
Add comment · Show 2 · 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 Alp-Giray-Savrum · Jan 28, 2014 at 11:47 AM 0
Share

Can i use Array $$anonymous$$ethod is$$anonymous$$d of List ?

avatar image moghes · Jan 28, 2014 at 11:52 AM 0
Share

Yes you can, and please tick as true if the answer answers you correctly :)

avatar image
1

Answer by Exalia · Jan 28, 2014 at 12:43 PM

If you want to put your GameObjects into an array, there are several ways depending on how your scene is set up and your preferences.

If your objects already exist in your scene you can make your array public and drag them into the array using the inspector. Just declare your array in a script and drag them in.

 public GameObject[] gameobjects;

If you are using prefabs you can use the same technique.

If you don't want to do it this way you can Tag your objects. Then iterate through the objects in your scene until you have the ones you want. This is okay for initialisation but will increase CPU thread time dramatically at runtime.

 GameObject[] gameobjects;
 
 void Start()
 {
   gameObjects = GameObject.FindObjectsWithTag("ArrayObjects");
 }

I hope this helps

P.S : Planes are once sided with Y value that defines which way it is facing

are you defining a plane or a volume?

(0.6,0.6,0.6) is the same as (0.6,1,0.6) in a plane, (0.6,-1,0.6) would mean it is facing the other way.

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 Alp-Giray-Savrum · Jan 28, 2014 at 03:26 PM 0
Share

Volume, i know it doesn't matter until plane's Y axis = 0.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate random object from Array 2 Answers

Spawn objects at random position ON a 2D object. 1 Answer

Invoke Ball Obstacle based on Score 0 Answers

random spawning of platforms. Overlapping and 2x2 spawning issue. 1 Answer

I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer


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