- Home /
How to change position of gameobjects in an asset bundle externally using a javascript
Hi guys. I have a project where Im using unity 3d editor and load different game objects outside unity 3d. ie using this code from web-browser :
u.getUnity().SendMessage("MyassetBundle", "LoadScene", "objname,objurl");
and here is my c# code :
IEnumerator LoadScene(string parameters){
string [] split = parameters.Split(new Char [] {','});
WWW www = WWW.LoadFromCacheOrDownload (split[1], 1);
yield return www; AssetBundle bundle = www.assetBundle;
GameObject objAsset = bundle.Load (split[0], typeof(GameObject)) as GameObject;
Instantiate (objAsset ); }
This works fine for me and object gets loaded into unity3d player. Now my requirement is that i need to change the position of these objects.I tried the same code by adding another function which is given below :
IEnumerator MoveObjects(string parameters){
string [] split = parameters.Split(new Char [] {','});
WWW www = WWW.LoadFromCacheOrDownload (split[1], 1);
yield return www; AssetBundle bundle = www.assetBundle;
GameObject objAsset = bundle.Load (split[0], typeof(GameObject)) as GameObject;
Instantiate (objAsset );
objAsset.transform.localScale += new Vector3(10F, 0, 0);
}
and call them as before :
u.getUnity().SendMessage("MyassetBundle", " MoveObjects", "objname,objurl");
But again the object gets instantiated but the action on object(here rotation) is not taking place. I also tried to attach a script at runtime to these objects.The code is given below :
//same code as above till Instantiate (objAsset );
objAsset .GetComponent<DynamicScript>.ParseObjects ();
where DynamicScript is the script i attached to the project which is as given below :
public class DynamicScript : MonoBehaviour { void Start () { } void Update () {
}
public void ParseObjects(){
transform.localScale += new Vector3(10F, 0, 0);
}
}
Even this didnt worked for me.Can anyone of you help me regarding this query.Thanks in advance
Answer by GameVortex · Jan 14, 2015 at 02:51 PM
Instantiate (objAsset );
objAsset.transform.localScale += new Vector3(10F, 0, 0);
Here objAsset is the Template you use to Create an object in the scene. After you call Instantiate the objAsset is still the Template and you try to change the scale of the template, which will not change the created object in the scene. The object created is returned by the Instantiate function, so you need to store that object in a variable and then use that variable to change the scale of the created object:
GameObject createdObject = Instantiate (objAsset ) as GameObject; //Instantiate return object of type Object, so wee need to convert it to GameObject.
createdObject.transform.localScale += new Vector3(10F, 0, 0);
Remember to use and study the API when coding in Unity: http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Answer by amenon · Jan 16, 2015 at 05:57 AM
Thank you sir.This worked for me, but a small problem : Here is my new code :
IEnumerator MoveObjects(string parameters){
string [] split = parameters.Split(new Char [] {','});
float xpos = float.Parse(split [2], CultureInfo.InvariantCulture.NumberFormat);
float ypos =float.Parse(split [3], CultureInfo.InvariantCulture.NumberFormat);
float zpos = float.Parse(split [4], CultureInfo.InvariantCulture.NumberFormat);
WWW www = WWW.LoadFromCacheOrDownload (split[1], 1);
yield return www;
AssetBundle bundle = www.assetBundle;
GameObject MechanicAsset = bundle.Load (split[0], typeof(GameObject)) as GameObject;
GameObject createdObject = Instantiate (MechanicAsset ) as GameObject;
createdObject.transform.position = new Vector3 (xpos, ypos,zpos);
}
Now the problem is everytime i need to reload the unity web player.otherwise the object is being recreated.ie another copy of same object is created. The gameobject already been created is not getting destroyed.Any method to destroy the previous object already loaded???