Hello,
Working on taking my game multiplayer and an existing script that was working is now not working and I am getting a null reference. I am familiar with null reference and requiring an instance of an object, but from what I can tell I have it set up correctly- clearly I am missing something though…
Here is the code, I have the function shoot, which calls the RPC Spawn Projectile. It was working fine before, but now as I have converted it to an RPC to use online, I am getting a null reference on this line:
myArrow.AddRelativeForce(Vector3.forward * power);
Here is the exact error message for reference:
NullReferenceException: Object reference not set to an instance of an object
ShootArrow.SpawnProjectile (Vector3 position, Quaternion rotation) (at Assets/_Scripts/ShootArrow.cs:82)
And here is the code: thank you very much in advance and if you can put anything in as to why the error is happening that will help me learn and ask less questions in the future, much appreciated again as always.
Code:
public void Shoot(float power)
{
launchPosition = cameraHeadTransform.TransformPoint(0,0,0); //where to launch arrow from
//instantiate arrow and add force to it based on the amount of power
networkView.RPC("SpawnProjectile", RPCMode.All, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x, myTransform.eulerAngles.y, 0));
//The below lines are how I had it set up before, if that is useful..
// Transform myArrow = (Transform)Instantiate(arrow, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x, myTransform.eulerAngles.y, 0));
//myArrow.rigidbody.AddRelativeForce(Vector3.forward * power);
}
[RPC]
void SpawnProjectile (Vector3 position, Quaternion rotation)
{
Rigidbody myArrow;
myArrow = Instantiate(arrow, position, rotation) as Rigidbody;
myArrow.AddRelativeForce(Vector3.forward * power);
Debug.Log ("code was reached");
}