Search Unity

Attach a script to a Gameobject via script

Discussion in 'Scripting' started by micuccio, Oct 23, 2014.

  1. micuccio

    micuccio

    Joined:
    Jan 26, 2014
    Posts:
    143
    Hi Guys,

    I am sure this question was raised multiple times, but please help me.

    I am trying to attach a script to a gameobject via script (by using addcomponent) but it does not work.
    Any suggestion?

    thanks
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    How is it not working? Do you get a crash? Does nothing happen?

    We can't give you any help if you don't provide the information needed to help you!
     
  3. micuccio

    micuccio

    Joined:
    Jan 26, 2014
    Posts:
    143
    Hey thank.
    You are right, let me be more specific:
    My steps are :

    1) I create a string array and here i store the name of the scripts to be attached

    2)I use the Gameobject.Addcomponent();






    and i get this : [Assets/Scripts/createGO2.cs (55,3)] Component Type 'counter' not found.


    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class createGO2 : MonoBehaviour {
    6.    
    7.    
    8.     private MeshCollider col;
    9.     public string counter;
    10.     //public Transform obj1;
    11.    
    12.    
    13.     //private GameObject[] meshers;
    14.     //store gameObject reference
    15.     GameObject objToSpawn;
    16.    
    17.    
    18.    
    19.     void Start()
    20.     {
    21.        
    22.        
    23.         for (int i = 0; i <3; i++)
    24.         {
    25.             int next=i+1;
    26.            
    27.            
    28.             string[] contatore = new string[4];
    29.             counter[0] = "Quad1Mesher1";
    30.             counter[1] = "Quad1Mesher2";
    31.             counter[2] = "Quad1Mesher4";
    32.             counter[3] = "Quad1Mesher5";
    33.             //alfa = contatore [i];
    34.            
    35.            
    36.            
    37.  
    38.             string namefile= counter[i];
    39.             Debug.Log(counter[i]);
    40.  
    41.             objToSpawn = new GameObject("objToSpawn"+i);
    42.             //Add Components
    43.             //objToSpawn.AddComponent<Rigidbody>();
    44.             objToSpawn.AddComponent<MeshFilter>();
    45.             //objToSpawn.AddComponent<BoxCollider>();
    46.             objToSpawn.AddComponent<MeshRenderer>();
    47.             objToSpawn.AddComponent("counter[i]");
    48.            
    49.            
    50.            
    51.            
    52.            
    53.            
    54.  
    55.  
    56.         }
    57.     }
    58.    
    59.    
    60.  
    61.    
    62.    
    63. }
     
  4. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
  5. micuccio

    micuccio

    Joined:
    Jan 26, 2014
    Posts:
    143
    It worked! Thanks!!!