Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

simple add component question

Discussion in 'Scripting' started by gibberingmouther, Nov 28, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Code (csharp):
    1.  GameObject object1;  // you need to get this initialized to something obviously
    2.  
    3. // ... then later in the code...
    4.  
    5. script1 myScript1Instance = object1.GetComponent<script1>();
    If you just want to add it and you don't want to do anything with it afterwards, you don't need the instance assignment, just start with the last part of that from object1 onward.
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Code (csharp):
    1.  
    2. object1.AddComponent<script1>();
    3.  
    script1's Awake, OnEnable and then Start would be called.