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 Rich_XR · Nov 25, 2020 at 07:41 PM · gameobjectinstantiateprefabscalecube

Instantiating Cube Prefabs with scale

How can I make the cube gameobject prefab instantiate so that they will always touch if i change the scale of the object?

Currently, the cubes are separated if i change their size. Apologies this is such a simple question:

 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class Gameplay : MonoBehaviour
     {   
     
         public Transform brick;
     
         public  int gridWidth=10;
         public int gridDepth=10;
         public int gridHeight=10;
     
         // Start is called before the first frame update
         void Start(){
     
             for (int y = 0; y < gridHeight; y=y+1) 
             {
                 for (int z = 0; z < gridDepth; z=z+1) 
                 {
                     for (int x = 0; x < gridWidth; x=x+1) 
                     {
                     Instantiate (brick, new Vector3 (x, y, z), Quaternion.identity);
                     }
                 }
             }
         }
     }
 

 

Essentially I want a 3d cube out of instantiated game objects cube, as many as I want through the inspector.

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DerDerErIst · Nov 26, 2020 at 01:49 AM

First You need to Define what the localsize of the Objects is:

Here is a simple Version, you have to Setup your Prefab correctly:
Make an Empty GameObject, then Add as Example a Cube to it. Make it a Prefab and Implement it in the Unity Inspector to the brickParent object.

Parent Objects should always be on Scale 1, so we gonna scale the localSize of the Child Object, scaling the Parent Objects can often causes huge problems later in development Stages.

     public class UnityAnswer : MonoBehaviour
     {
     //Setup the Prefab correct, Empty Gameobject LocalScale 1 and the Cube (other Object) as Child Object of the Empty GameObject
         public GameObject brickParent;
     
         public int gridWidth = 10;
         public int gridDepth = 10;
         public int gridHeight = 10;
     //Define the Size of your Objects
         public float localScale = .5f;
     
         // Start is called before the first frame update
         void Start()
         {
             for (int y = 0; y < gridHeight; y++)
             {
                 for (int z = 0; z < gridDepth; z++)
                 {
                     for (int x = 0; x < gridWidth; x++)
                     {
 //We Instantiate the GameObject and the Position of x/y/z * localScale so we can Access it
                         GameObject go = Instantiate(brickParent, new Vector3(x * localScale, y * localScale, z * localScale), Quaternion.identity) as GameObject;
 //Since we only have One Child (The Cube as Example) we get the First Child (Dont mess around with GetComponentInChildren because GetComponentInChildren doesnt really works like its Named it always will start with GetComponent and get the Parent Object
                         Transform t = go.transform.GetChild(0);
 //We Scale the Child Object to the new localScale
                         t.localScale = new Vector3(localScale, localScale, localScale);
                     }
                 }
             }
         }    
     }




Here is the same Approach just with 2 additional Methods to show you how you chould Change the Grid then in Runtime



 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Random = UnityEngine.Random;
 
 public class UnityAnswer : MonoBehaviour
 {
 
     public GameObject brickParent;
 
     public int gridWidth = 10;
     public int gridDepth = 10;
     public int gridHeight = 10;
 
     public float localScale = .5f;
 
     List<GameObject> gridObjects = new List<GameObject>();
 
     // Start is called before the first frame update
     void Start()
     {
         for (int y = 0; y < gridHeight; y++)
         {
             for (int z = 0; z < gridDepth; z++)
             {
                 for (int x = 0; x < gridWidth; x++)
                 {
                     GameObject go = Instantiate(brickParent, new Vector3(x * localScale, y * localScale, z * localScale), Quaternion.identity) as GameObject;
                     Transform t = go.transform.GetChild(0);
                     t.localScale = new Vector3(localScale, localScale, localScale);
                     gridObjects.Add(go);
                 }
             }
         }
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             RefreshLocalScale();
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             NewGrid();
         }
     }
 
     private void RefreshLocalScale()
     {
         localScale = Random.Range(.5f,2f);
         if (gridObjects.Count > 0)
         {
             foreach (var item in gridObjects)
             {
                 item.transform.position = new Vector3(item.transform.position.x * localScale, item.transform.position.y * localScale, item.transform.position.y * localScale);
                 Transform t = item.transform.GetChild(0);
                 t.localScale = new Vector3(localScale, localScale, localScale);
             }
         }
     }
 
     private void NewGrid()
     {
         localScale = 1.5f;
 
         if (gridObjects.Count > 0)
         {
             foreach (var item in gridObjects)
             {
                 Destroy(item);
             }
         }
         gridObjects.Clear();
 
         for (int y = 0; y < gridHeight; y++)
         {
             for (int z = 0; z < gridDepth; z++)
             {
                 for (int x = 0; x < gridWidth; x++)
                 {
                     GameObject go = Instantiate(brickParent, new Vector3(x * localScale, y * localScale, z * localScale), Quaternion.identity) as GameObject;
                     Transform t = go.transform.GetChild(0);
                     t.localScale = new Vector3(localScale, localScale, localScale);
                     gridObjects.Add(go);
                 }
             }
         }
     }
 }

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
0

Answer by mharris45 · Nov 26, 2020 at 12:10 AM

Why not add the cubes as a child of a Empty GameObject then just scale the empty?

This will be much faster than scaling each individually.

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 DerDerErIst · Nov 26, 2020 at 01:06 AM 0
Share

Thats and absolute "Easy Way" to do it, but You shouldnt do that, since it will give you weird Behaviours later in the Game when things are not scaled properly.

avatar image
0

Answer by Rich_XR · Nov 26, 2020 at 09:14 AM

Thanks for your time on this @DerDerErIst will implement today. Thank you!

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 Rich_XR · Nov 26, 2020 at 04:28 PM 0
Share

Hey @DerDerErIst this has worked! The only issue now is that it instantiates at 0,0,0 in the scene, even if i move the entire Gameobject with child cube and script into desired location?

avatar image DerDerErIst Rich_XR · Nov 26, 2020 at 07:36 PM 0
Share

it starts at 0 and ends at 10

you have to change your parameters if you want it at different location
the grid get build up with y/z/x on 0 increment the value by one multiplied by the scale


if you want your grid as example starting on x = 25 then you need to start your loop at 25

lets say as example:

 for (int x = 25; x < (gridWidth + 25); x++)

now he will start on x 25.


dont forget to mark my answer as solved please

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

260 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do I revert prefab instance properties to their defaults? 0 Answers

Thrown objects always have same rotation 2 Answers

Empty GOs as info storage 1 Answer

Cannot destroy child object in prefab- Error 1 Answer

Get scale of instantiated prefab and resize 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