Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[SOLVED] Button' does not contain a definition for `AddComponent'.

Discussion in 'Scripting' started by u_rs, Mar 23, 2018.

  1. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    I have 2 classes in the same script and I wan't to add "ButtonController" class to the button.
    Code (CSharp):
    1. private Button _newButton;
    2. ...
    3. _newButton.AddComponent<ButtonController>();
    But can't:
    Code (CSharp):
    1. Type `UnityEngine.UI.Button' does not contain a definition for `AddComponent' and no extension method `AddComponent' of type `UnityEngine.UI.Button' could be found.
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    790
    Because the class Button doesn't have any definition for AddComponent, you should be using the GameObject to do that _newButton.gameObject.AddComponent...
     
    SkyGhost and u_rs like this.
  3. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Right, thanks.