Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aidangig56 · Apr 24, 2017 at 11:57 PM · c#instantiateruntimeaddcomponent

Attach C# Script on runtime

I want to attach a C# script to a GameObject on runtime. The C# script that I want to attach is Instantiated with a GameObject called _RoomController(Clone)

Screenshot 1 alt text

Screenshot 2 alt text

  public string ClonedRoomController = "_RoomController(Clone)";
     public GameObject hardpoint1;
     public RoomController rc;
     // Use this for initialization
     void Start () {
         var RoomController2 = gameObject.AddComponent(Type.GetType(ClonedRoomController));
         RoomController2 = rc;
         RoomController2 = gameObject.AddComponent(Type.GetType(ClonedRoomController));
         if (rc.currentGameMode == "DOM")
         {
             hardpoint1.SetActive(true);
             Instantiate(hardpoint1, transform.position, transform.rotation);
         }
     }

I am trying to attach the C# Script with class RoomController to rc. I currently have to attach the script during runtime and hit pause then it works. I want to add the C# Script as soon as I play. The C# script currently is Instantiated with name _RoomController(Clone)

unity3d-c-question.png (17.8 kB)
unity3d-c-question2.png (22.0 kB)
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
3

Answer by Commoble · Apr 25, 2017 at 12:08 AM

To create and attach a script to a GameObject, use AddComponent:

 RoomController rcScript = hardpoint1.AddComponent<RoomController>();

The above line instantiates a RoomController script, attaches it to the rc GameObject, and stores a reference to it in the newly created rcScript variable.

The best way to use AddComponent is typically the generic version, which is the last variant on the documentation page.

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

8 People are following this question.

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

Related Questions

Instantiate and edit a text box during runtime 1 Answer

Can I use AddComponent to add (this.script) to an object? 1 Answer

Instantiate a prefab and add a script to it 1 Answer

C# Why do I have to use .AddComponent and not "new (ClassName)"? 1 Answer

Distribute terrain in zones 3 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