- Home /
Multiple instantiated objects, the first fail to scale
Hi all,
I have a code that loads assets from an asset bundle and then instantiate them using "instructions" from a JSON file (position, rotation and scale). Few of the assets are instantiated multiple times. My code looks like this (this is a simplified version):
void from_asset_bundle_set_items(SimpleJSON.JSONNode arr_objs, string str_tag = "")
{
//The code runs on the assets in the JSON and calls a function that instantiate each of them
foreach (SimpleJSON.JSONNode obj in arr_objs)
{
set_item(obj, str_tag);
}
}
void set_item(SimpleJSON.JSONNode obj)
{
//Get values from JSON file
string str_path = obj["path"].Value;
float float_position_x = float.Parse(obj["position"]["x"].Value);
float float_position_y = float.Parse(obj["position"]["y"].Value);
float float_position_z = float.Parse(obj["position"]["z"].Value);
float float_rotation_x = float.Parse(obj["rotation"]["x"].Value);
float float_rotation_y = float.Parse(obj["rotation"]["y"].Value);
float float_rotation_z = float.Parse(obj["rotation"]["z"].Value);
float float_scale_x = float.Parse(obj["scale"]["x"].Value);
float float_scale_y = float.Parse(obj["scale"]["y"].Value);
float float_scale_z = float.Parse(obj["scale"]["z"].Value);
Debug.Log(str_path + " in y scale " + float_scale_y);
//Load the asset from the bundle
GameObject item = m_asset_bundle.LoadAsset<GameObject>(str_path);
//Instantiate the object
Instantiate(item, new Vector3(float_position_x, float_position_y, float_position_z), Quaternion.Euler(float_rotation_x, float_rotation_y, float_rotation_z));
//*** Change the scale - works but not on the first TYPE of object *****
item.transform.localScale = new Vector3(float_scale_x, float_scale_y, float_scale_z);
//item.transform.parent = transform;
item.transform.SetParent(transform, false);
Debug.Log(str_path + " in y scale " + float_scale_y);
}
My problem is that the FIRST item of each certain TYPE of object isn't scaling.
For example, the FIRST of 4 WHITE closets isn't scaling. The others are ok...
Then, ONLY THE FIRST of WOOD closets isn't scaling again... The other 3 are ok.
I tried changing the scale variables in the code (above) with real values and the problem persists.
What am I doing wrong? How can I fix it?
Thank you!!
Doesn't item.transform.SetParent(transform, false) change the position and scale of the item? Have you tried either using true in SetParent() or setting the position, rotation, and scale after parenting to transform?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Refrencing Instantiated Object 1 Answer
Instantiate prefab using Resources.Load() 2 Answers
Having trouble instantiating a gameobject with rotation 0 Answers