Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by amenon · Jan 14, 2015 at 01:28 PM · assetbundleassets

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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???

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to import the object from server to unity 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Assetbundle 1 Answer

assemblies can not turn to bundle 2 Answers

how to unload assets which auto loaded by unity?,how to unload assets? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges