- Home /
prefab instance keeps its connection to script
I have a scene where I have a GUIText object which I have linked to my character prefab. When I instantiate this in game it creates a clone and the script doesn't work on the new object. If I pause the game and drag the prefab onto the GUIText script again and run it it will work fine. Is there something I need to be doing in my code to make sure I can keep this connection on any instances.
I have added the code it basically looks at the script PickupScore on my prefab which is what is losing it's connection.
var player : PickupScore;
function Update()
{
GetComponent(GUIText).text = player.score.ToString();
Debug.Log("added value");
}
Answer by Owen-Reynolds · Apr 08, 2013 at 02:09 PM
Prefabs, before they are spawned, can't have links to existing gameObjects. [Spawned prefabs can (since they are just regular game objects,) and prefabs can have links to other prefabs.]
To fix, set the link yourself when you spawn (rough code, my doctor won't let me use javascript):
newGUI = Instantiate(guiPrefab,....);
newGUI.GetComponent("PickupScore").player = GameObject.Find("player");
thanks I am not sure how my question got posted twice but yes this works. thanks