HOW to bind SCRIPT to an OBJECT on the stage SOFTWARE?

Quite recently I started learning Unity. Everything went quite smoothly and well, until it happened. I decided to do this: (the analogy is given so that it would be easier to understand):

1) Create a cube

2) I make a clone of this cube at the click of a button

3) Specify another script for this clone (when a clone is created, the script is not duplicated)

Screwed up at the 3rd step ¯_ (ツ) _ / ¯

1) The cube is already on stage 2) I duplicate it with Instantiate(myCube); 3) The cube from which copies are created simply moves around the stage (WASD), and the cubes that are duplicated change color / move erratically / increase 4) For each created cube, the action that it will perform is set randomly

 GameObject cube; ... void createCloneCube(){ GameObject clone; clone = Instantitate(cube) as GameObject; clone.GetComponent<BoxCollider>().enabled = true; //Тут должен быть скрипт добавления компонета Script к clone } 
  • Why the script is not duplicated? It should be a complete copy. How do you clone? What kind of scripts? Do they set completely different logic or have the same logic (like KnifeWeapon, GunWeapon, TomatoAttack)? What do these cubes generally do and mean? - Alexey Shimansky
  • you first need to bind the script, and then do the prebuff. In this case, everything should be duplicated - Garrus_En
  • Well, as usual, they will make a link to the GameObject / Transform (any reference object) in the script, and when cloning, the script is cloned, but the link remains to that GameObject / Transform, and then they send spam in the forums that "the script is not cloned", " Unity broke. " You need to know how to clone a GameObject from the scene, there is no such problem with the prefab. - Xumera_hZ
  • 1) The cube is already on stage 2) I duplicate it with Instantiate (myCube); 3) The cube from which the copies are created simply moves around the stage (WASD), and the cubes that are duplicated change color / randomly move / increase 4) For each created cube, the action that it will perform is set randomly - Alexander
  • You need to change this: prntscr.com/g94v7k - Alexander

2 answers 2

 GameObject cube; ... void createCloneCube(){ GameObject clone = Instantitate(cube) as GameObject; clone.GetComponent<BoxCollider>().enabled = true; "Компонент" addComp=clone.AddComponent<"Компонент">(); } 

There are 4 ways to attach a script (the same component) to an object:

one). button Add Component in the object editor

2). take the script and drag it to the object

3). in another script write [RequireComponent (typeof ("Component")))]

four). I wrote the last one above.

  • I need to attach another script to the clone - Alexander
  • @ Alexander, what's this? Make a prefab, an empty cube without a script, after cloning add the necessary script and you do not need to delete the previous one. - Xumera_hZ
  • How to add it (script) programmatically? - Alexander
  • @ Alexander "Component" addComp = clone.AddComponent <"Component"> (); - Xumera_hZ
  • I'm not talking about that. How to attach the script itself from Assets? C # script itself. As in the picture: prnt.sc/g94v7k - Alexander

Any script created automatically becomes a component. In the search for components, the script is displayed along with other (components)

To add a script program need:

  1. Create the script itself
  2. Specify in the code where the clone / prefab is created:

    clone.AddComonent <CubeScript> ();