Question by
Amaanzi · Dec 01, 2019 at 02:51 PM ·
rigidbodynetworkingrigidbody.addforce
NullReferenceException: Object reference not set to an instance of an object and networking
Hey there, I'm moderately new to Unity and its networking have researched to the best of my ability but have still been getting the error: NullReferenceException: Object reference not set to an instance of an object - when I am pretty certain I have referenced it in the code. the exact error is NullReferenceException: Object reference not set to an instance of an object PlayerMovement1.FixedUpdate () (at Assets/Scripts/LAN/PlayerMovement1.cs:24) The game still runs, but when I host it the player does not move - no force is added to move forward left or right. Is there anything I'm doing wrong, because in the inspector I've dragged the rigidbody into the box thingy. Thanks in advance!
public Rigidbody rb;
public float forwardforce = 200f;
public float sidewaysforce= 500f;
public bool MovingRight = false;
public bool MovingLeft = false;
public void Start ()
{
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void FixedUpdate() {
if (GetComponent<NetworkIdentity>().hasAuthority)
{
if(MovingRight)
{
MoveRight();
}
if(MovingLeft)
{
MoveLeft();
}
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
if(Input.GetKey("d"))
{
rb.AddForce(sidewaysforce,0,0 * Time.deltaTime);
Debug.Log("Moving Right");
}
if(Input.GetKey("a"))
{
rb.AddForce(-sidewaysforce,0,0 * Time.deltaTime);
Debug.Log("Moving Left");
}
//if (rb.position.y < -5f)
//{
//FindObjectOfType<GameManager>().EndGame();
//}
}
}
void MoveRight()
{
if (GetComponent<NetworkIdentity>().hasAuthority)
{
rb.AddForce(sidewaysforce,0,0 * Time.deltaTime);
}
}
void MoveLeft()
{
if (GetComponent<NetworkIdentity>().hasAuthority)
{
rb.AddForce(-sidewaysforce,0,0 * Time.deltaTime);
}
}
public void MoveLeftButton()
{
MovingLeft = true;
}
public void MoveRightButton()
{
MovingRight = true;
}
public void MovingRightFalse()
{
MovingRight = false;
}
public void MovingLeftFalse()
{
MovingLeft = false;
}
}
Comment