title: Unity Add Component
body: <p>I am having some trouble in unity with adding a script I wrote to a cube <strong>in runtime</strong>. I have seen lots of other people asking about this but they seem to have fixed it. </p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>            // Insert
              GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
              // Appearance
              cube.GetComponent&lt;Renderer&gt;().material.SetColor("_Color", Color.yellow);
              Shader transparent;
              transparent = Shader.Find("UI/Default");
              cube.GetComponent&lt;Renderer&gt;().material.shader = transparent;
              //
              //
              // Postioning
              //cxcxcxcxc
              cube.transform.position = new Vector3((xpos), (ypos), zpos);
              cube.transform.localEulerAngles = new Vector3(90, 0, 0);
  
  
              var myScript = cube.gameObject.AddComponent&lt;CoinCollect&gt;();
  </code></pre>
  
  <p>This code creates a cube, adds shaders and positions it. I would now like to add a script to it which I have created called "CoinCollect" which is <strong>done with the last line of code.</strong> This doesn't spring up any errors but when the game is run the script isn't added. Could someone help? I looked at the documentation but it mainly showed what I was doing.</p>