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 hollym16 · Feb 01, 2017 at 12:18 PM · instantiatespawndistanceint

Spawn a number of prefabs an equal distance apart

Hello All,

I have a script that looks at an int number (set by the user) and spawns that many prefabs on a button click. So far it works pretty well, but the prefabs all spawn on top of each other.

What do I need to add to make them spawn in a horizontal line?

Here's my functioning script C#:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SpawnMore : MonoBehaviour {
 
     public GameObject prefab;
     public int num = 1;
     public GameObject NumObj;
     private Text Numbers;
     private float distance = 0.5f;
 
     // Use this for initialization
     void Start() {
         Numbers = NumObj.GetComponent<Text>();
         Numbers.text = num.ToString();
     }
     public void Add()
     {
         if (num < 9)
         {
             num += 1;
             SetCountText();
         }
         else
         {
             return;
         }
         }
     
     public void Subtract()
     {
         if (num > 1)
         {
             num -= 1;
             SetCountText();
         }
         else { 
             return;
         }
     }
     void SetCountText()
     {
         Numbers.text = num.ToString();
     }
     public void SpawnX()
     {
         Vector3 pos = Camera.main.transform.position;
         Vector3 setRight = Camera.main.transform.right;
         Vector3 setForward = Camera.main.transform.forward;
 
         for (var i = 0; i < num; i++)
         {
             Instantiate(prefab, (pos + (setRight * distance) + setForward * 2), transform.rotation);
         }
     }
 }

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
0
Best Answer

Answer by Fobri · Feb 01, 2017 at 12:41 PM

int offset = 0; for (var i = 0; i < num; i++) { Instantiate(prefab, (pos + (setRight * distance) + setForward * 2) + new Vector3(offset, 0, 0), transform.rotation); offset += 30; }

Should work, not tested tho. modify the offset value at the end of the loop to spawn them closer/further from each other.

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 hollym16 · Feb 01, 2017 at 02:57 PM 0
Share

Brilliant, thank you very much

avatar image
1

Answer by repsacc · Feb 01, 2017 at 02:21 PM

You are spawning them in the same place. (pos + (setRight * distance) + setForward * 2) never changes. Try the code below instead.

 for (var i = 0; i < num; i++)
 {
     Instantiate(prefab, (pos + (setRight * distance) + setForward * i), transform.rotation);
 }

This will spawn the objects further and further in away from the main camera, in the forward direction of the main camera.

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 Bunny83 · Feb 01, 2017 at 02:39 PM 0
Share

Since he said "horizontal" i guess he ment on the x-axis as he already has a "distance" variable. So i guess he wants

 (setRight * distance * i) 

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

77 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

Related Questions

Trying to spawn enemies on only one path 1 Answer

instantiate capacity 0 Answers

How to do a Spawn Animation 1 Answer

Spawn Prefab based on int value 1 Answer

Im trying to set a spawned enemy a transform on another script 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