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 finae · Jan 05, 2014 at 06:09 AM · componentattach

scripts as a addcomponent to a gameobject

Hi,

I've read the reference to AddComponent but its not making sense and when using the mono editor, its still not making sense.

here's where i am stuck at:

 Apple A=new Apple;
 A.setName("Green Apple");
 
 GameObject Bob=(GameObject) Instantiate(Resources.Load("Bob"),Vector3.zero,Quaternion.identity);
 

how do I attach 'A' to Bob?

would:

Bob.AddComponent() <-- wouldnt this just attach a new script component to bob and not the one create 'A' above?

Bob.AddComponent("Apple") <--- isnt this the same as above , adding a new component of script Apple to Bob.

i m trying to figure out how to attach the already create 'A' to Bob.

Thanks

Comment
Add comment · Show 1
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 finae · Jan 05, 2014 at 02:39 AM 0
Share

Only way i can think of would be to add the script component first and then configure it afterward.

or add another add functions inside the script to return the private members and reassign to the BOB gameobject.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ncallaway · Jan 05, 2014 at 08:09 AM

I'm not sure of a way of precreating the Component and adding it to a specific object. You can use AddComponent to create a new component object, and then set the properties on it that you want.

 GameObject Bob=(GameObject) Instantiate(Resources.Load("Bob"),Vector3.zero,Quaternion.identity);
 Apple apple = Bob.AddComponet<Apple>();

 apple.setName("Green Apple");

As an post-script, I generally recommend using the generic version of AddComponent rather than the version that takes a string. It makes your code more type-safe, and makes it easier on the tooling if you want to later rename the "Apple" class to something else (maybe it becomes the Fruit class instead of the Apple class).

For more information check out http://docs.unity3d.com/Documentation/Manual/GenericFunctions.html and http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

Comment
Add comment · Show 1 · 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 finae · Jan 10, 2014 at 09:21 PM 1
Share

Thanks, I was hoping for a way to add the already created component and then assigning it as as the component is going to be configure during a routine, the code will just change so that the component is already added to the object.. also thanks for teh suggestion to use type rather than string.

avatar image
0

Answer by DTek · Mar 28, 2015 at 11:11 AM

If you need to keep track of certain content within the Component then use it later for the recently created game object, simply create a new component for the object and transfer the variables from the old component to the newly created one.

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

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

Related Questions

Script Component unchecks itself prior to build. 1 Answer

Object reference not set to instance of an object 2 Answers

Detect collision when changing character controller height 2 Answers

Delete a script component if object has already one 2 Answers

How do I replace a component with 'sent' component? Is this even possible in Unity? 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