Search Unity

Add one script to multiple gameobjects

Discussion in 'Scripting' started by mayursojitra, Feb 2, 2015.

  1. mayursojitra

    mayursojitra

    Joined:
    Jan 20, 2015
    Posts:
    2
    if i add script component to game objects and reference it with same script, does it instantiate multiple time or one time??
     
  2. DP00707

    DP00707

    Joined:
    Aug 13, 2014
    Posts:
    29
    If you add it to 2 separate Game objects, they are 2 instances of the same script. They can carry their own public values for variables.

    If you need to Instantiate both, you probably are best adding them both to a list in a separate script and access them in a foreach loop.
     
    Kiwasi likes this.
  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079