Hello there, I am currently sitting on a really crappy problem, below you see my code where the player gets spawned into the world (using the PUN Plugin) and that works fine, the camera gets attached as well and follows the player accordingly, but I just don’t get the InputScript to work.
void OnJoinedRoom()
{
player = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0); //Instantiate the Player Clone
Camera.main.GetComponent<CameraScript>().target = player.transform; //Let the Camera Target the player
InputScript.fetch_movement = true; // Start fetching input to move character
InputScript.target = player; // Give InputScript the player gameobject
}
It gives ingame the error that: “Object Reference is no set to an Instance of an Object”… and I tried various things but I can’t get it to work how I want it…
Here’s my InputScript:
public class InputScript : MonoBehaviour
{
public static bool fetch_movement;
public static GameObject target;
void Update ()
{
if(fetch_movement)
{
if(Input.GetKeyDown(KeyCode.A))
{
target.GetComponent<PlayerMovement>().moving_left = true;
}
if(Input.GetKeyUp (KeyCode.A))
{
target.GetComponent<PlayerMovement>().moving_left = false;
}
if(Input.GetKeyDown(KeyCode.D))
{
target.GetComponent<PlayerMovement>().moving_right = true;
}
if(Input.GetKeyUp (KeyCode.D))
{
target.GetComponent<PlayerMovement>().moving_right = false;
}
if(Input.GetKeyDown(KeyCode.W))
{
target.GetComponent<PlayerMovement>().moving_up = true;
}
if(Input.GetKeyUp (KeyCode.W))
{
target.GetComponent<PlayerMovement>().moving_up = false;
}
if(Input.GetKeyDown(KeyCode.S))
{
target.GetComponent<PlayerMovement>().moving_down = true;
}
if(Input.GetKeyUp (KeyCode.S))
{
target.GetComponent<PlayerMovement>().moving_down = false;
}
}
}
}
Hint: The game is 2D, in case you were wondering why W is used for UP and S for DOWN.
I appreciate any kind of help/ideas!