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 orjn01 · Feb 28, 2012 at 07:00 PM · javascriptrandom

Help with spawning an object in a random location

Hi

I'm new to this and I could need a little help with my script. It's supposed to spawn an object in a random location between the coordinates (1,0.5,1) and (29,0.5,29) and if that object gets destroyed it should spawn a new one in a new random location. This is the script I have made. It seems that it only spawn the object in the location (0,0,0). A little help would be appreciated.

     #pragma strict
 
 function Start () {
 }
 
 var gameobject : GameObject;
 var location : Vector3 = Vector3(0,0.5,0);
 var xaxis : int=15;
 var zaxis : int=15;
 
 
 function Update () {
 
 var xaxis=Random.Range(1,29);
 var zaxis=Random.Range(1,29);
 
 var location : Vector3 = Vector3(xaxis, 0.5, zaxis);
 if(transform.childCount < 1)spawn();
     
 
 }
 function spawn () {
 var Instance : GameObject = Instantiate(gameobject, location, transform.rotation);
 Instance.transform.parent = transform;
 }

 
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

1 Reply

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

Answer by Kleptomaniac · Feb 29, 2012 at 08:35 AM

Your problem is that you are defining location, xaxis and zaxis all twice. Although all those variables are being changed, because they are being redefined as themselves, var Instance is simply interpreting it as what is defined outside of Update(), which is Vector3(0,0.5,0).

Basically delete your variable definitions of location, xaxis and zaxis outside of Update() and it should now work properly for you. :)

Klep

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 orjn01 · Feb 29, 2012 at 10:27 AM 0
Share

Thanks a lot.

avatar image Kleptomaniac · Feb 29, 2012 at 08:21 PM 0
Share

No worries! ;) Glad to help.

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

Generate a random number that is different every time? 1 Answer

How to spawn objects in a specific range of random location 1 Answer

Expressions in statements must only be executed for their side-effects. 1 Answer

3d text will not display float 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