Instantiate with parameter not working

This not working ang giving me error:

//Ignore random spawn position in instantiate its not relevant
GameObject ship = Instantiate(spawnableShip_01,new Vector3(transform.position.x + Random.Range(-5,5),transform.position.y + Random.Range(-5,5),0),Quaternion.identity) as GameObject;
    
ship.GetComponent<SpaceShip_Trader>().cameFromStargate = true;

It will even spawn the ship and then always always no matter how else I set it:

NullReferenceException: Object reference not set to an instance of an object

Does your spawnableShip_01 prefab have a script named SpaceShip_Trader on it? And does that have a public property called cameFromStargate?

1 Answer

1

The ship gets instantiated but ship.GetComponent() = null.

This means your ship does not have the component SpaceShip_Trader attached to it, because your prefab (spawnableShip_01) does not have it.

Totaly right. Sorry I'm from different engine so I'm not used to prefabs etc.