2 basic questions on scripting

1. Is it possible to add a new component to a GameObject using scripting?
I have a mesh where I have removed the Mesh Renderer-component and now I want to add it again using javascripting…but the manual only mentions GetComponent…

2. What’s the best way to have variables that are globally accessible in Unity?
I want to have some variables that all my scripts can “see”, no matter which gameobjects they are are attached to.

Cheers
/lars

As for (1), I think you are looking for AddComponent:

As for (2): Good question! Until now I just queried the object that I needed, and then accessed its variables, but indeed global vars would be nice, a kind of a “world” object.

global variables are the same as static variables.

In a script named StaticSample.js

static var test = 10;

From another script:

print(StaticSample.test);

Ah! Learning everyday.