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:
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?
Why does it take so long for the scale to "apply/show up".
==UPDATE==
As per comments, here's a detailed description of my scene:
1 camera, 1 terrain (with a bunch of brushed in deformations), 1 directional light. I also have a .blend file in my Assets folder, which is automatically imported and I get my aStarCellHighlight prefab which is a cube with a transparency/diffuse texture:
to the camera I have attached to following script, to which I've then "bound" the terrain and aStarCellHighlight in the corresponding public fields:
public class RTSCamera : MonoBehaviour {
public Collider terrain;
public GameObject aStarCellHighlightPrefab;
private bool isCursorOnTerrain;
private RaycastHit cursorPositionOnTerrain;
private GameObject aStarCellHighlight;
void Start () {
cursorPositionOnTerrain = new RaycastHit();
aStarCellHighlight = (GameObject)Instantiate(aStarCellHighlightPrefab, terrain.transform.position, terrain.transform.rotation);
aStarCellHighlight.name = "cellHighlight";
aStarCellHighlight.transform.parent = terrain.transform;
}
void Update () {
trackCursorOnTerrain(false);
placeCellHighLight();
}
private void trackCursorOnTerrain(bool debug) {
Ray ray = this.camera.ScreenPointToRay(Input.mousePosition);
isCursorOnTerrain = terrain.Raycast(ray,out cursorPositionOnTerrain, 5000);
if(debug) {
if(isCursorOnTerrain) {
Debug.DrawLine (ray.origin, cursorPositionOnTerrain.point);
Debug.Log("terrainRight="+cursorPositionOnTerrain.point.x+" terrainUp="+cursorPositionOnTerrain.point.z+" terrainAltitude="+cursorPositionOnTerrain.point.y);
} else {
Debug.DrawLine (ray.origin, ray.GetPoint(5000));
}
}
}
private void placeCellHighLight() {
aStarCellHighlight.transform.localScale = new Vector3(50,50,200);
if(isCursorOnTerrain) {
aStarCellHighlight.transform.localPosition = cursorPositionOnTerrain.point + new Vector3(0,200,0);
} else {
aStarCellHighlight.transform.localPosition = Vector3.zero + new Vector3(0,200,0);
}
}
}
The script is supposed to basically draw a ray from camera's mouse cursor screen position to terrain, and then instantiate the Blender cube prefab, add is at a child of terrain, scale it to a visible size, and move it around on the terrain to where the mouse is, you know , "projecting" or whatever it is called.
when I hit play I the Blender prefab instance is created right away, it is shown as a descendat of terrain, is just not scaled properlly. Also if I move the mouse right away, the cube doesn't move with it:
Finally, if I just wait 10-20 seconds, the cube gets scaled, and now it moves with the cursor on the terrain:
Thanks for all any help!