How do I access the instance of a Prefab?

This is probably a bit of a newbie question, but I'm new to C# and the whole prefabs thing.

I want to change a variable of an instantiated prefab from within another script. I know that I can do this with a GameObject that is already in the hierarchy, but how do I do this with an instance of a prefab. Here's what I am trying:

    private ShipScript ship;

    // Use this for initialization
    void Start ()
        ship = (ShipScript)GameObject.Find("ShipUnityPrefab").GetComponent("ShipScript");

    void doSomething ()
        ship.variableA = 2;

It gives me a "NULLReferenceException Object reference not set to an instance of an object" error. The ShipUnityPrefab is instantiated just fine, I just can't seem to access it.

Thanks! -JJ

You've got to be instantiating the prefab in some other script's Start(). That's not going to work unless the other script's code ran first, you'll get your error. And you can't order the execution of Start()s. You could instantiate in Awake() for a quick fix, but that's not a good long-term solution. You should eventually learn to use events, and send the instantiated prefab out, to whatever needs to know about it.

Also, use the generic form of GetComponent to eliminate clutter, and don't use the version that takes a string as its parameter, for performance, and more clutter elimination:

ship = GameObject.Find("ShipUnityPrefab").GetComponent<ShipScript>();