How do I add a specific instance of a Component to an object after I instantiate it?

I think it should be pretty straightforward, but I have tried a lot of different things and nothing has worked. I have a Game Manager with a script GameManagerScript. Inside that script I have a variable public RoomScript roomToPlaceScript. I want to be able to add roomToPlaceScript to an object after I instantiate it. Kind of like this (but this didn’t work).

GameObject newRoom = (GameObject)Instantiate(roomPrefabsWithoutScripts[nextRoomArrayElement], newRoomPosition, Quaternion.Euler( new Vector3(0f ,-90 * roomToPlaceScript.numRotations, 0f)));
newRoom.transform.parent = roomsInDungeonHolder.transform;
RoomScript a = newRoom.AddComponent("RoomScript") as RoomScript;
a = roomToPlaceScript;

The above partially worked. It attached a RoomScript as a Component, but it only had values for some variables (I’m pretty sure just the variables that were changed in the GameManagerScript).

I have also tried stuff like this

newRoom.AddComponent("roomToPlaceScript") as RoomScript;

but nothing is working. Any help would be greatly appreciated.

well when you instantiate an object you need to store a reference to it , once you’ve done that just use c# gameObject.AddComponent()javaScriptgameObject.AddComponent("roomToPlaceScript") that should work, and to the above not adding values well you can set those values via code ,for instance if you wanna change a float just use newroom.GetComponent("RoomScript").yourfloat = 10f; otherwise you’ll have to write it in the code like var float:myfloat = 10 in your newroom script.just a note im not fluent in javascipt so there may be errors just comment if you need help with it.hope ive helped solve the problem.