Instantiating a spaceship prefab. cannot access then change the prefabs settings

This is my script.

using UnityEngine;
using System.Collections;

public class Spaceship: MonoBehaviour

public int playerID;
public int yOffset;
public GameObject currentSpace;
//public Transform SpaceShip;

// Use this for initialization
void Start () 


// Update is called once per frame
void Update () 


public static void buildShip ()
	GameObject shipPrefab = (GameObject)Instantiate(Resources.Load("Spaceship"));
	//shipPrefab.playerID = 0;


I want to be able to set the variables : playerID, yOffset and curretSpace after its been instantiated.

Can anyone help ?

Using Csharp

The way you have things structured here is a bit strange to me. Pressing forward and assuming the “Spaceship” object that you load from Resources has a ‘Spaceship’ script on it, you would do it this way:

GameObject shipPrefab = Instantiate(Resources.Load("Spaceship")) as GameObject;
Spaceship spaceship = shipPrefab.GetComponent<Spaceship>();
spaceship.playerID = 1234;
spaceship.yOffset = 11;

One issue is that the function you are using to create the spaceship is inside the Spaceship script. So you likely have to add parameters to the ‘buildSpaceship’ function for what values you want to set for things like playerID and yOffset.

What is “Spaceship” in your script? Do you actually have a monobehaviour named Spaceship?
Perhaps try

shipPrefab.GetComponent(typeof(Spaceship)) as Spaceship;