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
1
Question by qwertyk31 · Jul 16, 2013 at 09:30 AM · javascriptcomponent

Add a script to a gameobject that doesn't exist yet

I have a script that creates a cube but I need to add a script to that newly created cube. How would i do this?

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
3
Best Answer

Answer by CHPedersen · Jul 16, 2013 at 09:40 AM

Right after you create the cube (presumably using GameObject.CreatePrimitive?) you can add whatever component you want, including scripts, by calling AddComponent on the newly created object.

Like so:

     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     cube.AddComponent<YourScriptHere>();
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 qwertyk31 · Jul 16, 2013 at 10:01 AM 0
Share

Thanks for the answer. I really appreciate the vote up too.

avatar image CHPedersen · Jul 16, 2013 at 10:34 AM 0
Share

No problem - welcome to Unity Answers!

avatar image qwertyk31 · Jul 16, 2013 at 12:01 PM 0
Share

when trying it I get "Assets/Standard Assets/Scripts/my scripts/Box$$anonymous$$aker.js(10,9): BCE0005: $$anonymous$$ identifier: 'Cube'." here is my code. error is on line 10

 //to be used later
 var cubeSpeed = 5;
 
 function Start () {
     //makes cube
     var ss : GameObject  = GameObject.CreatePrimitive(PrimitiveType.Cube);
     //moves it to player
     ss.transform.position = Vector3(1010.457, 3.351044, -584.6373);
     //adds script to cube
     Cube.AddComponent(Box$$anonymous$$aker);
 }
 
avatar image amphoterik · Jul 16, 2013 at 12:05 PM 1
Share

You want to do:

 ss.AddComponent(Box$$anonymous$$aker);

You are doing

 Cube.AddComponent(Box$$anonymous$$aker);

See the difference?

avatar image Bunny83 · Jul 16, 2013 at 12:17 PM 0
Share

sigh

     ss.AddComponent(Box$$anonymous$$aker);
Show more comments
avatar image
0

Answer by TobyXD · Jul 16, 2013 at 09:44 AM

Use GameObject.AddComponent API to add Script to a GameObject.

In C#

 ScriptClass scriptVar = (ScriptClass)newGameObject.AddComponent<ScriptClass>();

In Js

 var scriptVar = newGameObject.AddComponent(ScriptClass);

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

19 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 avatar image avatar image

Related Questions

How do I disable Script on FP Controller? (Solved) 2 Answers

destroy hinge joint component from another object in javascript 2 Answers

Why am I getting this error ? 2 Answers

Activate a component when an animation clip in an animator is played? 0 Answers

Script.Enabled does not work in Unity 5+ 2 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