Difference between a script in the scene and in instantiated

I have a script about GUIText.

public GUIText Enemyhealth;
private int EnemyH = 300;
private float distance = 2f;

Start {
Enemyhealth.text = " " + EnemyH;

Update {

if (Vector.Distance(transform.position, Vector.zero) < distance)
EnemyH = EnemyH - 50;
Enemyhealth.text = " " + EnemyH;

And it works perfectly - when both Enemyhealth ( GUIText object ) and object ( to which the script is attached to ) are on the scene.

But when i try to simply add
“GUIText EnemyHealth = Instantiate ( Enemyhealth ) as GUIText;”
trying to actually instantiate both GUIText and the object ( to which script is attached to ), it don’t work.

  1. It shows GUIText , but with a state from the last play before. If the last time i played it was 250, then only the next time i play it shows 250.

  2. And it don’t change the value in the game itself. If 250 should become 200, it stills shows 250, while in prefab it actually changed to 200. But the change itself will be shown only the next time i play. Without changing it in play mode at all.

So, as i said, it works perfectly when both GUIText object and the object ( to which scrpit is attached to ) are in the scene, but it’s broken when they are instantiated. Why?

To get a reference to the script you will want to do the following:

instantiate as a gameobject

I had the same problem at one point. This is just psuedo code, but you have all of the parts there, you just need to arrange it differently, and when instantiating, get the game object, and then pull the script component out of the newly created gameobject. from there you can update the values in Enemyhealth.

Not sure if this helps, but from what I understand of your code and question, this may solve the problem.