Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by horrorsaur · Jan 18, 2017 at 04:17 AM · 2d gamespawnpositioning

4 spawn locations, 4 prefabs

Good evening all,

I'm trying to set up a script that spawns 4 random prefabs at 4 locations i have set up as spawn locations. When I play the game, the objects all instantiate but two errors occur... 1) spawns 4 objects of the same color (but the color can change every time I rerun the game but still 4 of the same color) 2) they all spawn in the same location.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BucketSpawn : MonoBehaviour {
 
     [SerializeField]
     private GameObject[] spawnLocations;
 
     [SerializeField]
     private GameObject[] bucketPrefabs;
 
     private int bucketsOnScreen = 0;
 
     // Use this for initialization
     void Start () {
         int rndSpawnLocation = Random.Range(0, 3);
         int rndBucketSpawn = Random.Range(0, 3);
 
         while(bucketsOnScreen < 4) {
             Instantiate(bucketPrefabs[rndBucketSpawn], spawnLocations[rndSpawnLocation].transform);
             bucketsOnScreen++;
         }
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
 }

i've literally spent hours trying to figure out whats going on, and why it wont actually fetch a position for each individual piece rather than obtaining a random position for everything and setting it for all 4 pieces. i've tweaked it so much that now I cant even get them to spawn on the screen anymore. any help would be greatly appreciated.

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
0

Answer by TheCallipo · Jan 18, 2017 at 09:46 AM

The Start() Function only executes one time at the start of the scripts lifetime. Thats why the Random.Range Function only provides one number to your rndVariables.

Put the code in the Update() Function. The Update() Function is called every frame in the scripts lifetime.

Also make sure you access the "spawnLocations"-GameObjects position with spawnLocations[rndSpawnLocation].transform.position

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 horrorsaur · Jan 18, 2017 at 10:36 PM 0
Share

Thank you for the reply! I've went ahead and moved my script to the Update function. It would appear it is still not working. It will spawn 4 buckets in the same location and the same color. Any ideas? @TheCallipo

Also, I used this to Instantiate the objects, they are now showing up.

 Instantiate(bucketPrefabs[rndBucketSpawn], spawnLocations[rndSpawnLocation].position, spawnLocations[rndSpawnLocation].rotation);

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

92 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to spawn prefabs with the same speed as the prefabs spawned before? 0 Answers

avoid spawning objects on other ones 1 Answer

Buy system - transform.position bought object 0 Answers

is there a way to randomize the position transition of an object within a limited area? (using 2D sprite) 0 Answers

Making the player go from one scene to another but spawn in a certain place 0 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