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 Basicman · Jul 29, 2014 at 04:23 PM · positionrandomspawn

Spawn a gameobject in random position within an area?

hello all, the problem is I have create some prefab gameobject and place them on different position, and I use OnTriggerEnter destroy them and spawn them, however I keep destroy and spawn again again and again, the gameobject starting out of my area, so how can I spawn them in random position within an area, can somebody help me with this case, thank a lot! Sorry for my poor english, hope you guys understand.

here is my script:

 public GameObject cube;

     void SpawnCube(){

     Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 1, Random.Range(-10.0F, 10.0F));
     Instantiate (cube, position, Quaternion.identity);
 }


     void OnTriggerEnter(Collider collider){
     if (collider.gameObject.name == "player") {
         Destroy (this.gameObject);
         SpawnCube();
         
     }
 }
Comment
Add comment · Show 3
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 KiraSensei · Jul 29, 2014 at 04:27 PM 0
Share

Something is weird in your code : you destroy the game object that handles this script and then you try to call a method from it. This can't work. You should do the spawn before destroying the game object.

I assume the cube is the same prefab that contains this script ?

avatar image $$anonymous$$ · Jul 29, 2014 at 04:33 PM 0
Share

$$anonymous$$iraSensei is right but ins$$anonymous$$d, what if you create Empty GameObject which will spawn your prefabs as its childs components so that way, you can store them into some List and destroy them dynamically?

avatar image Basicman · Jul 30, 2014 at 11:34 AM 0
Share

Thanks for reply. Yes, the cube is same prefab and contains this scripts. But my main problem is like I made a area like (50X50), and I place the prefab in middle like vector3(0,0,0), I use the random.range make the spawn location from origin prefab -10f to 10f in axis Z, so it may change to vector3(0,0,10), and do it again, it may go vector3(0,0,20), and keep doing this, it may go vector3(0,0,50 or more), so the prefab is out of my area, so how can I random spwan them in my (50X50) area? ZyTr0n, Are you means create a empty GameObject to fixed the origin position to spawn the prefabs? thanks you guys!!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by 432qwerty · Jul 29, 2014 at 04:39 PM

All you have to do is swap the line Destroy (this.gameObject); and the line SpawnCube(). You can't create another cube when you have destroyed this one and therefore destroyed this instance of the script before it creates the new cube! As for the positioning, check the scaling of your prefab.

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 Basicman · Jul 30, 2014 at 11:13 AM 0
Share

thank you for your suggestion, can let me know more, thanks

avatar image
1

Answer by jmgek · Jul 29, 2014 at 04:41 PM

       Destroy (this.gameObject);
         SpawnCube();

You are calling this script over and over again while the player is in the trigger, destroy and then spawn.

I don't fully know what you are looking for but try this. Try using.

 void OnTriggerExit(Collider collider){
     if (collider.gameObject.name == "player") {
         SpawnCube();
     }
 
    void OnTriggerEnter(Collider collider){
     if (collider.gameObject.name == "player") {
         Destroy (this.gameObject);
     }
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 Basicman · Jul 30, 2014 at 11:15 AM 0
Share

thank a lot, your scripts is better, I will try this one, thanks

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

7 People are following this question.

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

Related Questions

Quaternion.identity problem at random spawn 1 Answer

Spawn game object in random position on screen 1 Answer

How do i spawn a square in random position within an area but only at certain x and y intervals? 1 Answer

Randomly Place Cubes In Viewport Without Overlap 1 Answer

Generate random number and set a GameObject active. 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