Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
7
Question by Obsdark · Feb 04, 2016 at 10:44 PM · c#gameobjectscript.runtimecomponents

How to add a Script to a GameObject during Runtime [2016]

I try to search for this but most of methods out there (Google, other questions, etc[...]) aren't properly updated, because they use ways now deprecated to do this and the last version of the editor which is what i'm using right now (to this day, 01-02-2016, version 5.3.1) is not accepting them like valid ones or updating them like it should.

I saw on the internet too, in the official tutorials something about doing through generic methods, but i don't have any idea how to make it work out and i'm pretty much a newbie in C#.

Could you please show me one example of how to use generics to insert an specific script through (i suspect) addComponent to a GameObject?

Just for the sake of argument, i'm adding the script where i'm trying to set this to work, take in consideration than (in this case) is an object than generate objects:

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
 
     // Use this for initialization
     void Awake () {
         Vector3 vecAwake = new Vector3 (1, 1, 0);
 
         GameObject BuildModeController = new GameObject ();
 
         this.addBuilder (BuildModeController, vecAwake);
     }
 
     void addBuilder(GameObject go, Vector3 vecAwake){
         go.transform.SetParent (this.transform, true);
         go.transform.position = vecAwake;
         Debug.Log (go.name);
 
         //This doesn't work
         go.AddComponent<ScriptableObject> (go.name+".cs"); 
         // i also trying adding this, and using it through string 
                 //(method also by today, deprecated):
         //"Assets/Scripts/Controllers/Controller.cs (23,3)", ");
     }
 }

Thanks in advance

PS: Feel free to ask or request any kind of clarification or comment than help to improve quality of this question too.

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 feiting · Feb 05, 2016 at 12:48 AM 0
Share

http://answers.unity3d.com/questions/471531/add-component-script.html duplicate, one of many duplicates of this exact question, actually. The reference on GameObject will have AddComponent() too. This used to be in the scripting guide, probably moved it over to the tutorial section though, haven't checked

3 Replies

· Add your reply
  • Sort: 
avatar image
28

Answer by felixfors · Feb 08, 2016 at 09:11 PM

 public GameObject test;
 
 void Start()
 {
 test.AddComponent<ScriptName>();
 }
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 Guhanesh · Feb 09, 2016 at 07:28 AM

go.AddComponent(); //put your script name inside

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 IssataySC · Jun 01 at 03:56 AM

If your script lies within certain namespace, you should follow the following format

 GameObject.AddComponent(typeof(namespace.className))`;
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

Shader.Find() equivalent for Compute Shaders? 2 Answers

Initiate class array at start and assign class values. 0 Answers

Having trouble swapping GameObjects on button click 1 Answer

how can i access to the scripts which have same name and were attached to the same gameobject. 2 Answers

Check for empty gameobject in script? 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