- Home /
Question by
RetnuhStudio · Jun 18, 2013 at 04:40 AM ·
instantiategetcomponentrpc
Null Reference Exception on instantiated objects script.
I am instantiating an object over a Network using an RPC. The object instantiates fine but if I try accessing the script when it is instantiated I get a null reference exception on the client and server.
This is the RPC.
// Instantiate a Arrow projectile across the network.
[RPC]
void SpawnProjectileArrow(Vector3 position, Quaternion rotation) {
GameObject arrow = Instantiate(projectileArrowPrefab, position, rotation) as GameObject;
arrowProjectileScript aScript = arrow.GetComponent<arrowProjectileScript>();
}
The "arrowProjectileScript" is attached to the projectileArrowPrefab.
Any Ideas on what is causing this?
Thanks - Matt
Comment
at runtime check the instantiate object. if the script isn't here the null exception is normal.
public Transfrom arrowProjectileScript;
It is assigned to the script calling the RPC by a public transform. But the prefab instantiates fine. I just cant access its script.
Answer by RetnuhStudio · Jun 19, 2013 at 04:53 AM
Dam, found it. Needed to be a public GameObject not a Transform. Whoops.
public GameObject arrowProjectileScript;
Thanks for the help guys.
Regards - Matt