public GameObject hazard; Rigidbody gm=Instantiate (hazard, spawnPosition, spawnRotation) as Rigidbody; gm.AddForce(0f,50,0f); I am trying to get id of created object and add force to it. Above code is giving object reference not set to instance of object error. How do i do this
With "as" you are trying to typecast your GameObject to a Rigidbody. But it's not a Rigidbody (even if it has a Rigidbody component). So the typecast will return null. I think you mean to do: Code (CSharp): GameObject noob = Instantiate(hazard, spawnPosition, spownRotation) as GameObject; Rigidbody gm = noob.GetComponent<Rigidbody>(); gm.AddForce(0, 50, 0); (Though adding a force for a single frame like this won't have a very good effect — you generally need to add force over several frames. For a newly spawned object, you're often better off simply setting the rigidbody velocity instead.)