• 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 zer0c00l42066 · Nov 25, 2013 at 04:34 PM · webplayergameobjects

How do i create a gameobject, add a script to it, and change a public variable on that script. all from js

How do i create a gameobject, add a script to it, and change a public variable on that script. all from js

Here is my JS:

 #pragma strict
 
 function Start () {
     var testobj : GameObject;        
     testobj = new GameObject ("test");
     testobj.AddComponent ("OBJ");
     var targetComp : script = testobj.GetComponent(OBJ);
     OBJ.objPath = "http://www.everyday3d.com/unity3d/obj/monkey.obj" ;
 }
 
 function Update () {
 
 }

I get an error about the code before i can run it stating:

Assets/OBJ/src/createobj.js(7,26): BCE0018: The name 'script' does not denote a valid type ('not found'). Did you mean 'UnityEditor.ScriptCallOptimizationLevel'?

this is a unity3d webplayer app

Any help would be great ty

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Nov 25, 2013 at 04:38 PM

Assuming 'OBJ' is the name of the script you are trying to add and that 'objPath' is not static, you can do it this way:

 #pragma strict
  
 function Start () {
     var testobj : GameObject;   
     testobj = new GameObject ("test");
     testobj.AddComponent (OBJ);
     var targetComp : OBJ = testobj.GetComponent(OBJ);
     targetComp.objPath = "http://www.everyday3d.com/unity3d/obj/monkey.obj" ;
 }
Comment
Add comment · Show 6 · 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 zer0c00l42066 · Nov 25, 2013 at 04:48 PM 0
Share

TY for your quick response. yes your assumptions are correct. However with these changes i get an error stating:

Assets/OBJ/src/createobj.js(7,22): BCE0018: The name 'OBJ' does not denote a valid type ('not found').

And the script im trying to add is OBJ.cs what does the "Bug19" do in this example?

avatar image AlucardJay · Nov 25, 2013 at 04:49 PM 0
Share

Line 6 seems to have gone funny! I'm assu$$anonymous$$g it was UA auto-formatting of some kind.

 // testobj.AddComponent (Bug19);
 testobj.AddComponent (OBJ);

Surely there could be a better name for the script than OBJ zer0c?

avatar image zer0c00l42066 · Nov 25, 2013 at 05:01 PM 0
Share

Im still getting the error :Assets/OBJ/src/createobj.js(7,22): BCE0018: The name 'OBJ' does not denote a valid type ('not found'). its like its looking for the script that wont be added till runtime.. the script is located under assets:OBJ:src folder

avatar image AlucardJay · Nov 25, 2013 at 05:06 PM 1
Share

So it seems you are using both C# and uJS. This is going to lead to headaches, you are better off just working in one language. The problem is the Script Compilation Order. Check my answer here on how to use both languages in the same project : http://answers.unity3d.com/questions/385582/how-to-connect-2-scripts-using-c-js.html

And be wary of the point about when you do this, only one language will be able to see the other e.g. uJS can see C#, but C# cannot see uJS.

Edit : all the Unity Documentation links have changed, here is the link : http://docs.unity3d.com/Documentation/$$anonymous$$anual/ScriptCompileOrderFolders.html

avatar image robertbu · Nov 25, 2013 at 05:14 PM 0
Share

I ran a quick test on the code before I posted it. Since I did not have an 'OBJ' script, I put in a script I did have ('Bug19') and I missed changing one back when I changed them back for posting.

As for fixing the 'valid type' problem, on the few occasions I've mixed languages, I tossed the C# in StandardAssets as indicated in the link @alucardj provided.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the Performance implications of stacking gameobjects and switch between them for character customization. 1 Answer

Combine two or more gameobjects with different rigidbody values 0 Answers

How do I check if a boolean variable is true on a group of gameobjects with the same script? 1 Answer

Problem with installing Unity Webplayer 0 Answers

Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer


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