- Home /
 
 
               Question by 
               kekikcilerakin · Jan 03, 2016 at 09:04 AM · 
                scripting problemscript.scriptingproblemscripterror  
              
 
              Add script to created gameobject via script
 GameObject.AddComponent
 
               Hi, I have this c# code. It is creating random cubes and spheres. I have a script named "destroy". I want to add this "destroy" script to below code. I gave new material but I can't give this script.
 using UnityEngine;
 using System.Collections;
 
 public class RandomSpawn : MonoBehaviour {
     float toplam = 0;
     int a = 1;
     // Use this for initialization
     void Start () {
         
     }
     void Update(){
         toplam += Time.deltaTime;
         if (toplam >= 1) {
             int x = Random.Range (-8, 9);
             int z = Random.Range (-4, 5);
             if (a == 1) {
                 GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
                 cube.AddComponent<Rigidbody> ();
                 cube.transform.position = new Vector3 (x, 0.5f, z);
                 MeshRenderer mr = cube.GetComponent<MeshRenderer>();
                 int b = Random.Range (1, 5);
                 if (b == 1) {
                     mr.material = Resources.Load("cubeBlue", typeof(Material)) as Material;;
                 }
                 else if (b == 2) {
                     mr.material = Resources.Load("cubeGreen", typeof(Material)) as Material;
                 }
                 else if (b == 3) {
                     mr.material = Resources.Load("cubeRed", typeof(Material)) as Material;
                 }
                 else if (b == 4) {
                     mr.material = Resources.Load("cubeYellow", typeof(Material)) as Material;
                 }
                 a = 0;
 
             } else if(a == 0) {
                 GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
                 sphere.AddComponent<Rigidbody> ();
                 sphere.transform.position = new Vector3 (x, 0.5f, z);
                 MeshRenderer mr = sphere.GetComponent<MeshRenderer>();
                 int b = Random.Range (1, 5);
                 if (b == 1) {
                     mr.material = Resources.Load("sphereBlue", typeof(Material)) as Material;;
                 }
                 else if (b == 2) {
                     mr.material = Resources.Load("sphereGreen", typeof(Material)) as Material;
                 }
                 else if (b == 3) {
                     mr.material = Resources.Load("sphereRed", typeof(Material)) as Material;
                 }
                 else if (b == 4) {
                     mr.material = Resources.Load("sphereYellow", typeof(Material)) as Material;
                 }
                 a = 0;
                 a = 1;
             }
             toplam -= 1f;
         }
     }
 }
 
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Making a character jump and crouch using a Joystick 1 Answer
GetComponent won't work 1 Answer
Why won't my scripts work? 1 Answer
Activate/Deactivate Scripts form another Script 2 Answers
Visual Studio Code Intellisense 0 Answers