- Home /
how add script by script to gameObject
Hi, I write a game based on Unity 3.x... and i have a problem with mini game in coconut shy. I win a power cell but i can't take it. I think that this script doesn't duplicate object with script attached to him.
static var targets : int = 0;
static var haveWon : boolean = false;
var winSound : AudioClip;
var cellPrefab : GameObject;
function Start () {
}
function Update () {
if(targets==3 &&haveWon == false){
targets=0;
audio.PlayOneShot(winSound);
winCell : GameObject = transform.Find("powerCell").gameObject;
winCell.transform.Translate(-1,0,0);
Instantiate(cellPrefab, winCell.transform.position, transform.rotation);
Destroy(winCell);
haveWon = true;
}
}
How in diffrent way I can add script to power cell witch I win?
Try to add your script onto Cell prefab or add that script in runtime with AddComponent().
I have script into prefab but unity make duplicate of gameObject witch is a element of coconutshy, so I can't add script (duplicate is created during play). I thinking about in place of winCell insert new power cell - prefab with script. but I don't quite know how to do.
done :) all components I add only to prefab, other than in book :) and it work!
Answer by EggQuiz857 · Oct 18, 2014 at 04:51 PM
What i would do is insted of spawning in a script why not just enable/disable it.
it is simple.
hoped this helped
Your answer
![](https://koobas.hobune.stream/wayback/20220613165324im_/https://answers.unity.com/themes/thub/images/avi.jpg)