Search Unity

Resolved Scaling problem when creating gameobject with script

Discussion in 'Scripting' started by iimuli, May 20, 2022.

  1. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16
    Hey! I'm trying to create tiles with script, but I'm having a problem where the scaling of the gameobjects gets weird once i hit play, they dont correspond to the set scale even remotely, i tried changing scales in code but they remain enormous for some reason while inspector shows the set 1, 0.1f, 1 as scale. I created a default cube with xyz=1 for comparison in play mode, here's the code im using. What am i doing wrong ?

    EDIT: extra question, is there any difference in performance if i instantiate prefab or create new gameobjects with scripts when the map gets bigger?

    (the "TileMouse" script seen in inspector is irrelevant, mouse over code for color change, tried disabling it also)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     Mesh mesh;
    9.  
    10.     [SerializeField]
    11.     Material material;
    12.  
    13.     int posX;
    14.     int posZ;
    15.     Vector3 tileScale = new Vector3(1, 0.1f, 1);
    16.  
    17.     private void Awake()
    18.     {
    19.         int tiles = 4;
    20.         posX = 0;
    21.         posZ = 0;
    22.         for (int i = 0; i < tiles; i++, posX += 2)
    23.         {
    24.             CreateTileMap();
    25.         }
    26.     }
    27.  
    28.  
    29.     void CreateTileMap()
    30.     {
    31.         var tile = new GameObject("Tile");
    32.         tile.AddComponent<MeshFilter>().mesh = mesh;
    33.         tile.AddComponent<MeshRenderer>().material = material;
    34.         tile.AddComponent<BoxCollider>();
    35.         tile.AddComponent<TileMouse>();
    36.         tile.transform.localPosition = new Vector3(posX, 0, posZ);
    37.         tile.transform.localScale = tileScale;
    38.     }
    39. }
    40.  



    nimetön.png
     
    Last edited: May 20, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Should you not just be making a prefab and instantiating that?
     
  3. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16
    yes i did that earlier and it worked fine, im just trying things out and trying to learn more of Unity and C#, and figure out why this is happening
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Well you're only setting the vertical scale (y axis). They're already flat planes so scaling them vertically won't change anything.

    In any case, I appreciate the desire to learn, but making game objects and manually applying all the components via code is not the way to go. I would stick to Instantiate(), which exists for exactly what you're doing.
     
    iimuli likes this.
  5. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16

    I found the reason, I set the Mesh as Plane in GameManager, changing it to Cube fixed the weird scaling issue. But I hear you, I'll stick to instantiating as it seems far more easier :)