Question by
duvalj18 · Dec 25, 2017 at 08:17 PM ·
networkingmultiplayernon-static
An Object Reference is required to access non-static member in a Multiplayer game,'Object Reference Required to acces Non-Static Member' Networking
I'm fairly new to Unity, so I'm sorry if this question seems silly. I'm trying to work on separating instances of my game running on a server. In other words, I'd like to only be able to control the player in the active window, instead of both. To fix this I tried following some post somewhere but I'm pretty sure it's from unity 4 or earlier. I'm getting the error on line 44, 'if(networkView.isMine) etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float speed = 10f;
Rigidbody rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody> ();
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
if (stream.isWriting) {
syncPosition = rigidbody.position;
stream.Serialize (ref syncPosition);
} else
{
stream.Serialize (ref syncPosition);
rigidbody.position = syncPosition;
}
}
void InputMovement()
{
if (Input.GetKey(KeyCode.W))
rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime);
}
// Update is called once per frame
void Update()
{
if (NetworkView.isMine)
{
InputMovement();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiplayer Game Networking Layer Problem 0 Answers
Unet: How can I know the percentage of packets lost? 1 Answer
Unet - spawning bullets, two issues 1 Answer
Multiplayer First Person shooter similar to Battlefield 1 Answer
How to smoothly interpolate game object positions in a network game? 0 Answers