• 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 Jrobson · Apr 26, 2014 at 06:50 PM · transform.positionrandom.range

assign AI start position randomly with an array of GameObjects

I've created 6 empty gameobjects scattered around my scene.My code is

 public class StartPositionScript : MonoBehaviour {
 
     public GameObject[] startPoint;
     public GameObject enemyObject;
     
     void Start () {
 
     enemyObject.transform.position = startPoint[Random.Range(0, startPoint.Length)].transform.position;
 
     }
 
 }

The AI and startpoint gameobjects are assigned correctly in the inspector but something isn't working. My enemy AI does move to different positions when I start the game but only to 3 and none of them are where I've placed startpoints. Any suggestions?

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 Carbongrip · Apr 26, 2014 at 06:57 PM 0
Share

but only to 3

Thats because your Random.Range is 0 thru start point which happens to be 3. So the outcome will always be any number between 0 and 3.

avatar image Jrobson · Apr 27, 2014 at 12:49 PM 0
Share

I don't follow. Where is startpoint defined as 3? Also surely it would still position itself AT one of my first 3 empty gameobjest startpoints - but it isnt. Its choosing 3 points but I cant see how its choosing them.

avatar image Jrobson · May 07, 2014 at 03:08 PM 0
Share

I found reference to "Set" and tried:

enemyObject = transform.position.Set(startPoint[Random.Range(0,startPoint.Length)]);

but get the error CS1501: No overload for method Set' takes1' arguments so assume in using Random.Range I'm assigning too many arguments for Set. Is there an alternative?

avatar image Lo0NuhtiK · May 07, 2014 at 04:04 PM 0
Share

On your enemyObject, do you have some script attached that is setting/changing it's position as well? Just drag a plain old cube onto your enemy object slot ins$$anonymous$$d of your actual enemy object and see what happens with that. --and change this spawn script back to the way you had it in your original post.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

2D - Move an object 1 Answer

AddForce to a randomly selected GameObject with a rigidbody 0 Answers

list of objects and place them in specific locations 1 Answer

How to teleport 4 objects in an array to 4 out of 8 possible locations 2 Answers

Generating an object in a random y position? c# 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