maintain script on different scenes

How can I maintain script which is attached to the game object over different scene.

GameObject testobj;

void Start()
{
      testobj = new GameObject("Test");
testobj.Addcomponent<TestScript>();
}

how can I maintain TestScript which contains the connection to the server and don't want to lose during the next scene loading.

DontDestroyOnLoad(testobj) doesnt work.

all object will be destroyed when a new level is loaded. if you want an object to be permanent and not destrooyed when you call LoadLevel, you should call DontDestroyOnLoad for the object. a code like this

DontDestroyOnLoad(testObject); //should be called in Start

Hi Thet,

Have you tried something like this?

testobj.AddComponent("MyServerScript");

I am waiting for your reply.