Unity scaling instantiated GameObject at Start() doesn't "keep"

Posted by Shivan Dragon on Game Development See other posts from Game Development or by Shivan Dragon
Published on 2012-11-17T17:08:23Z Indexed on 2012/11/17 17:22 UTC
Read the original article Hit count: 399

Filed under:
|
|

I have a very simple scenario:

  • A box-like Prefab which is imported from Blender automatically (I have the .blend file in the Assets folder).

  • A script that has two public GameObject fields. In one I place the above prefab, and in the other I place a terrain object (which I've created in Unity's graphical view):

    public Collider terrain;

    public GameObject aStarCellHighlightPrefab;

  • This script is attached to the camera.

The idea is to have the Blender prefab instantiated, have the terrain set as its parent, and then scale said prefab instance up.

I first did it like this, in the Start() method:

void Start () {
        cursorPositionOnTerrain = new RaycastHit();
        aStarCellHighlight = (GameObject)Instantiate(aStarCellHighlightPrefab, new Vector3(300,300,300), terrain.transform.rotation);
        aStarCellHighlight.name = "cellHighlight";
        aStarCellHighlight.transform.parent = terrain.transform;
        aStarCellHighlight.transform.localScale = new Vector3(100,100,100);
    }

and first thought it didn't work. However later I noticed that it did in fact work, in the sense where the scale was applied right at the start, but then right after the prefab instance came back to its initial scale.

Putting the scale code in the Update() methods fixes it in the sense where now it stays scaled all the time:

void Update () {
     aStarCellHighlight.transform.localScale = new Vector3(100,100,100);
     //...
}

However I've noticed that when I run this code, the object is first displayed without the scale being applied, and it takes about 5-10 seconds for the scale to happen. During this time everything works fine (like input and logging, etc). The scene is very simple, it's not like it has a lot of stuff to load or anything (there's a Ray cast from the camera on to the terrain, but that seems to happen without such delays).

My (2 part) question is:

  1. Why doesn't it take the scale transform when I do it at the beginning in the Start() method. Why do I have to keep scaling it in the Update() method?

  2. Why does it take so long for the scale to "apply/show up".

© Game Development or respective owner

Related posts about unity

Related posts about gameobject