Hiding the MeshRenderer on an Instantiated object

I have a GameObject prefab of a weapon, which is instantiated to the player when picked up.
I get the Object reference not set to an instance of an object error on my code. Please help me?

var P90 : GameObject = Instantiate(WeaponPrefab,Vector3(0,0,0),player.transform.rotation); // instantiated object.
var P90mesh : Component = P90.GetComponent(MeshRenderer); //Get the Component for the MeshRenderer.
P90mesh.active = false; //Hide the mesh of the weapon so the player does not see the transition to the parents.
P90.transform.parent = player.transform; //Set the parent to the player's arms.
player.BroadcastMessage("IndexUp"); //Switch the weapon so two arent on top of each other.
P90mesh.active = true; //enable it again.

I get the error when setting the P90mesh.active to either true or false.
Could anyone help me?

The problem is you are setting P90mesh to the wrong type, it should be type MeshRenderer, not Component… Also, we use “enabled” for a component (“active” for a GameObject):

var P90 : GameObject = Instantiate(WeaponPrefab,Vector3(0,0,0),player.transform.rotation); // instantiated object.
var P90mesh : MeshRenderer = P90.GetComponent(MeshRenderer); //Get the Component for the MeshRenderer.
P90mesh.enabled = false; //Hide the mesh of the weapon so the player does not see the transition to the parents.
P90.transform.parent = player.transform; //Set the parent to the player's arms.
player.BroadcastMessage("IndexUp"); //Switch the weapon so two arent on top of each other.
P90mesh.enabled = true; //enable it again.

this fixes your code, but this is all pointless anyways, as you are turning the mesh renderer off and back on again within a SINGLE FRAME, without even really doing much else in between… Also, I don’t think you want to Instantiate it at Vector3(0,0,0)… That’s the world origin. You should instead use the position of the arm…

You could reduce this all to just this:

    var P90 : GameObject = Instantiate(WeaponPrefab,player.transform.position,player.transform.rotation); // instantiated object.       
    P90.transform.parent = player.transform; //Set the parent to the player's arms.
    player.BroadcastMessage("IndexUp"); //Switch the weapon so two arent on top of each other.

there would be no noticeable difference in the Mesh Renderer