Search Unity

add script to GameObject at runtime

Discussion in 'Scripting' started by pKallv, Aug 20, 2018.

Thread Status:
Not open for further replies.
  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I am trying to add a script to my GameObject at run time:

    Code (CSharp):
    1. card.AddComponent<KD_CardScript>();
    The script is stored in the editor but not in a GameObject. How do i refer to the script so the code can find it?
     
    zhiligeng likes this.
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    I don't really get the issue. After executing the code you can see the component / script being added to the GameObject or not? If it has been added, you can simply get and use it (as always) by using GetComponent<KD_CardScript>()
     
    zhiligeng likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Is
    card
    an instance of a game object or is it a prefab? If you add it to a prefab (a dubious thing to do in my humble opinion) at runtime, then all currently instantiated objects will not contain the change made in the prefab.

    However, to access a script in an object you use GetComponent, e.g.
    var script = cardInstance.GetComponent<KD_CardScript>();
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Note that when you use AddComponent, it returns the instance of the script that you add. So if you need access to it right away...

    Code (CSharp):
    1. KD_CardScript newScript = card.AddComponent<KD_CardScript>();
    Otherwise, as mentioned, GetComponent to get the script somewhere else. Or, you can also set the value of AddComponent to a class variable instead to keep the reference and just access it from there.
     
    MrRadoman likes this.
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Ok let me try to explain a bit better.

    1) I create an object at runtime
    2) I have a script (KD_CardScript) that residing in the editor and is not attached to anything
    3) I want to attach KD_CardScript to the object that i have created using
    Code (CSharp):
    1. card.AddComponent<KD_CardScript>();
    4) When is run this i get the following message:
    The type or namespace name `KD_CardScript' could not be found. Are you missing an assembly reference?

    5) I have tried to reference to KD_CardScript in editor but failed. It is stored in "Assets/Scripts". I have never done this before.

    I hope this is a bit clearer.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Is KD_CardScript in a namespace?
    Is the spelling the same? (including caps) of the class name and the script file name.

    If using Visual Studios, do you see an error on the line, if so, right clicking on the error and selecting the lightbulb should offer you some fix suggestions also.
     
    Joe-Censored likes this.
  7. mrbloodmoney898

    mrbloodmoney898

    Joined:
    Mar 3, 2020
    Posts:
    1
    A shame this was never responded to with a satisfactory response, I have this exact problem.
     
    yungchewbacca likes this.
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You either have a typo or you're missing a
    using SomeNamespace;
    statement.
     
    Fenikkel likes this.
  9. dnamarcel

    dnamarcel

    Joined:
    Jul 2, 2015
    Posts:
    2
    Try duplicating or moving the original script to same folder of your main project.
    Some plugins downloaded from the web don't have namespaces.
     
  10. Mr_TEDDY

    Mr_TEDDY

    Joined:
    Feb 3, 2020
    Posts:
    2
    I had the same issue. In my case the problem was that the script I tried to add wasn't driving from mono behaviour. Maybe that will help.
     
  11. x1alphaz1

    x1alphaz1

    Joined:
    Dec 19, 2020
    Posts:
    23
    upload_2021-2-18_2-4-6.png upload_2021-2-18_2-4-6.png First check if ur script is working by attaching it manually... if it does ... then it should work like u said.
    card.AddComponent<KD_CardScript>();

    Moreover if u have public variables (example Health or prefabs) inside that script(in ur case it's KD_CardScript) u can simply assign them as : card.AddComponent<KD_CardScript>().health = 100;
     
    zytep and pengwei3190 like this.
  12. IssataySC

    IssataySC

    Joined:
    Apr 26, 2022
    Posts:
    1
    It is possible that you are like me did not add namespace while attempting to add script component. So here is the way I wrote it:

    GameObject.AddComponent(typeof(namespace.className));
     
  13. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    151
    Make sure that you are adding classes inheriting from MonoBehaviour.

    I've implemented this and it works fine:
    Code (CSharp):
    1. MyClass _myObject = gameObject.AddComponent<MyClass>();
    2. // Awake() and Start() are executed here.
    3. _myObject.SomeMethod();
     
Thread Status:
Not open for further replies.