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 /
This question was closed May 02, 2020 at 12:36 PM by Morphosys for the following reason:

Other

avatar image
1
Question by Morphosys · Apr 28, 2020 at 02:25 PM · c#2dinstantiate

Question about Instantiate

Hello and thanks for stopping by, here's my scene starting point :

alt text

I'm trying to instantiate prefabs in my scene and make them spawn around the initial prefab (the square in the image).

I've got a somewhat working code that does instantiate the prefab under it, and I now know how to make it spawn around. The problem is that it's not scalable : I would have to write in my code the position of each prefab...

Any ideas on how to do that ? Thanks for reading.

(Here's my current code) :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawning : MonoBehaviour {
 
     public GameObject Tile;
 
     [SerializeField] private int TotalTilesSpawned = 0;
     [SerializeField] private float xAxis = 0f;
     [SerializeField] private float yAxis = - 2f;
 
     void Update(){
 
         if (Input.GetKeyDown("s")){
          Instantiate(Tile, new Vector2 (xAxis, yAxis), transform.rotation);
          TotalTilesSpawned += 1;
          yAxis -= 2;}
 
     }
 
 }

scene1.png (4.9 kB)
Comment
Add comment · Show 1
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 Mrnoobik · Apr 28, 2020 at 02:28 PM 0
Share

Are you want to scale tile?

1 Reply

  • Sort: 
avatar image
1

Answer by Mrnoobik · Apr 28, 2020 at 02:36 PM

You can do this:

 public List <Vector2> positions = new List<Vector2>();

And then in Update():

 Instantiate(Tile, new Vector2 (xAxis, yAxis), transform.rotation);
 positions.Add (new Vector2(xAxis, yAxis));

Hope this helps you.

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 Morphosys · Apr 28, 2020 at 03:07 PM 1
Share

Thanks for answering ! Your solution helps keeping track of the positions of each instantiated tile but doesn't solve my initial problem : how can I make them spawn all around the first tile in an infinite number without having to write it's coordinates for each tile ? Example : Here's the scene when starting : alt text And here's the scene after some tiles got spawned : alt text Please excuse my lack of photoshop skills ^^

Thanks for the help !

scene1.png (4.9 kB)
scene2.png (15.6 kB)

Follow this Question

Answers Answers and Comments

153 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 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

HELP, Scripts dont work after instantiation. 0 Answers

How to move Instantiated 2D objects by 0.5 using arrows(or mouse) 1 Answer

Instantiate ground/enemies 1 Answer

Instantied 2D Prefab Is Invisible 1 Answer

Spawning prefabs dependant upon Health UI? 2 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