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 /
This question was closed Oct 21, 2016 at 04:59 AM by janicevelarde for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by janicevelarde · Oct 20, 2016 at 01:44 PM · instantiateposition

Instantiate object at certain position

Hi, I've been trying to instantiate an object in a certain position (for example at the center but retaining the spaces in between each cell) I'm new in unity and I'm having a hard time manipulating the code. Thankyou for for help. :)

using UnityEngine; using System.Collections;

public class GridGenerator : MonoBehaviour {

 public int width;
 public int height;
 

 public Transform BoardGenerator;
 public Transform cell;

 
 // Use this for initialization
 void Start () {
     GenerateGrid();
 }

 void GenerateGrid() {
     for (int x = 0; x < height; x++) {
         for (int y = 0; y < width; y++) {
             BoardGenerator = Instantiate (cell.gameObject).transform;
             BoardGenerator.parent = transform;
             BoardGenerator.position = new Vector3(88 * x, 88 * y, 0);

         
         }

     }


 }

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by danivdwerf · Oct 20, 2016 at 01:54 PM

try this:


using UnityEngine; 
using System.Collections;
public class GridGenerator : MonoBehaviour 
{
     private int width;
     private int height;
     private GameObject BoardGenerator;
     [SerializeField]private Transform cell;
     private void Start () 
     {
         width= whatYouWant;
         height=whatYouWant;
          GenerateGrid();
     }
     private void GenerateGrid() 
     {
         for (int x = 0; x < height; x++) 
         {
              for (int y = 0; y < width; y++) 
             {
                  BoardGenerator = Instantiate (TheObjectYouWantToInstantiate,TheTransFormYouWant,Quaternion.Identity) as GameObject;              
             }
         }
     }
}

Looking at your code, you should also look up the terms OOP and SRP.

Good Luck!

Comment
Add comment · 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
1

Answer by janicevelarde · Oct 20, 2016 at 02:30 PM

Thankyou very much ! :) @danivdwerf

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 danivdwerf · Oct 20, 2016 at 02:59 PM 0
Share

no problem

avatar image danivdwerf · Oct 20, 2016 at 04:29 PM 0
Share

@anicevelarde you should close the question so people with the same issues know the answer works

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiated prefab has a z-axis of 90 0 Answers

Instantiated prefab trying to keep weird Y position 0 Answers

How to store a position that is moving to create a prefab to spawn at the exact position 1 Answer

Instantiate prefab if no prefab exists at location 1 Answer

Convert Local Position To Global on a Child Object 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