- Home /
Accessing Script on instated prefab
I have a C# script in unity which is instantiating a prefab. (This works fine). Once This happens I need to access an array, stored on a script, on that prefab.
public static void roomBuilder(Vector3 loc)
{
if(roomcount < 20)
{
GameObject newRoom;
int roomNum = (int)(Random.Range(0, staticRoomArray.Length));
newRoom = Instantiate (staticRoomArray[0]) as GameObject;
Component value = newRoom.GetComponent<Room_adder>();
Debug.Log(value.doorArray[0]);
newRoom.transform.position = (newRoom.transform.position) + loc;
}
}
This is the function Instantiating the prefab.
The instantiated prefab has a script with a "public GameObject[] doorArray;".
Any thoughts on how to make this work?
Answer by zee_ola05 · Feb 05, 2014 at 06:23 AM
Get access to the script of newRoom.
TheScript script = newRoom.GetComponent<TheScript>();
Then use this to access the public field.
script.doorArray
Your answer
Follow this Question
Related Questions
How to create GameObject without adding it to scene? 1 Answer
Spawning a prefab at another object's location 3 Answers
Issue Instantiating prefab in C# 0 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer