Need help with logical Problem (AddComponent)

Hello I have some problems with AddComponent

I have an NPC QuestScript and a GUI script

When I click the NPC, the QuestScript will tell the GUI script the name of the quest. I use AddComponent in QuestScript for this. But the Problem is, that it adds the component ever time I click the npc.

So I just added an Static Var in the Gui, and an IF check in the QuestScript if theres any QuestWindow allready opened.

But this dosnt stop adding the Component =(. So I was thinking about Object.Destroy but where to place that?

Here are the 2 scripts, hope you understand my bad english.


Script attached to the NPC

function OnMouseDown(){
if(QuestWindow.ShowQuestWindow != true){
    questWindows = gameObject.AddComponent("QuestWindow");

Script attached to the Camera

static var ShowQuestWindow : boolean = false;
var QuestName = "";

public function setQuestText(questnameinput){

QuestName = questnameinput;
ShowQuestWindow = true;


function OnGUI(){
    if(ShowQuestWindow == true){
        GUI.Label(Rect(0, 0, 200, 200), QuestName);
        if(GUI.Button(Rect(0, 25, 200, 200), "Close")){
            ShowQuestWindow = false;

All you need to do to check if a component exists on a certain object is to attempt to get it. If you get nothing in return, the component doesn't exist, and you can then add it.

Like this:

if(myGameObject.GetComponent(typeof(QuestWindow)) == null)
     // The component/script "QuestWindow" doesn't exist
     // on the game object "myGameObject" here.

This isn't exact code, you're going to need to adapt it to suit your needs, but this way you can check if the script is added already.

Ok Ive fixed it myself by using this code:

So Ive moved the AddComponent to the var declaration (Outside OnMouseDown)

var questWindows;
questWindows = gameObject.AddComponent(QuestWindow);

function OnMouseDown(){